home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / textutl3.lha / textutils-1.3 / lib / regex.c < prev    next >
C/C++ Source or Header  |  1992-06-29  |  156KB  |  4,862 lines

  1. /* Extended regular expression matching and search library,
  2.    version 0.4.
  3.    (Implements POSIX draft P10003.2/D11.2, except for multibyte characters.)
  4.  
  5.    Copyright (C) 1985, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #if defined (_AIX) && !defined (REGEX_MALLOC)
  22.   #pragma alloca
  23. #endif
  24.  
  25. #define _GNU_SOURCE
  26.  
  27. /* For interactive testing, compile with -Dtest.  Then this becomes
  28.    a self-contained program which reads a pattern, describes how it
  29.    compiles, then reads a string and searches for it.  If a command-line
  30.    argument is present, it is taken to be the value for obscure_syntax (in
  31.    decimal).  The default is 0 (Emacs-style syntax).
  32.    
  33.    If DEBUG is defined, this prints many voluminous messages about what
  34.    it is doing (if the variable `debug' is nonzero).  */
  35.  
  36.  
  37. /* The `emacs' switch turns on certain matching commands
  38.    that make sense only in Emacs. */
  39. #ifdef emacs
  40. #include "config.h"
  41. #include "lisp.h"
  42. #include "buffer.h"
  43. #include "syntax.h"
  44.  
  45. /* Emacs uses `NULL' as a predicate.  */
  46. #undef NULL
  47.  
  48. #else  /* not emacs */
  49.  
  50. /* POSIX.1 says that <unistd.h> might need <sys/types.h>.  We also need
  51.    it for regex.h.  */
  52. #include <sys/types.h>
  53.  
  54. #ifdef HAVE_UNISTD_H
  55. #include <unistd.h>
  56. #endif
  57.  
  58. #if defined (USG) || defined (POSIX) || defined (STDC_HEADERS)
  59. #ifndef BSTRING
  60. #include <string.h>
  61. #define bcopy(s,d,n)    memcpy ((d), (s), (n))
  62. #define bcmp(s1,s2,n)    memcmp ((s1), (s2), (n))
  63. #define bzero(s,n)    memset ((s), 0, (n))
  64. #endif /* not BSTRING  */
  65. #endif /* USG or POSIX or STDC_HEADERS  */
  66.  
  67. #ifdef STDC_HEADERS
  68. #include <stdlib.h>
  69. #else /* not STDC_HEADERS */
  70. char *malloc ();
  71. char *realloc ();
  72. #endif  /* not STDC_HEADERS */
  73.  
  74. /* If debugging, we use standard I/O.  */
  75. #ifdef DEBUG
  76. #include <stdio.h>
  77. #endif
  78.  
  79. /* Define the syntax stuff for \<, \>, etc.  */
  80.  
  81. /* This must be nonzero for the wordchar and notwordchar pattern
  82.    commands in re_match_2.  */
  83. #ifndef Sword 
  84. #define Sword 1
  85. #endif
  86.  
  87. #ifdef SYNTAX_TABLE
  88.  
  89. extern char *re_syntax_table;
  90.  
  91. #else /* not SYNTAX_TABLE */
  92.  
  93. /* How many characters in the character set.  */
  94. #define CHAR_SET_SIZE  256
  95.  
  96. static char re_syntax_table[CHAR_SET_SIZE];
  97.  
  98. static void
  99. init_syntax_once ()
  100. {
  101.    register int c;
  102.    static int done = 0;
  103.  
  104.    if (done)
  105.      return;
  106.  
  107.    bzero (re_syntax_table, sizeof re_syntax_table);
  108.  
  109.    for (c = 'a'; c <= 'z'; c++)
  110.      re_syntax_table[c] = Sword;
  111.  
  112.    for (c = 'A'; c <= 'Z'; c++)
  113.      re_syntax_table[c] = Sword;
  114.  
  115.    for (c = '0'; c <= '9'; c++)
  116.      re_syntax_table[c] = Sword;
  117.  
  118.    re_syntax_table['_'] = Sword;
  119.  
  120.    done = 1;
  121. }
  122.  
  123. #endif /* not SYNTAX_TABLE */
  124.  
  125. #define SYNTAX(c) re_syntax_table[c]
  126.  
  127. #endif /* not emacs */
  128.  
  129.  
  130. /* Get the interface, including the syntax bits.  */
  131. #include "regex.h"
  132.  
  133.  
  134. /* isalpha(3) etc. are used for the character classes.  */
  135. #include <ctype.h>
  136. #ifndef isgraph
  137. #define isgraph(c) (isprint (c) && !isspace (c))
  138. #endif
  139. #ifndef isblank
  140. #define isblank(c) ((c) == ' ' || (c) == '\t')
  141. #endif
  142.  
  143. #ifndef NULL
  144. #define NULL 0
  145. #endif
  146.  
  147. #ifndef SIGN_EXTEND_CHAR
  148. #ifdef __CHAR_UNSIGNED__    /* for, e.g., IBM RT */
  149. #define SIGN_EXTEND_CHAR(c) (((c)^128) - 128) /* As in Harbison and Steele.  */
  150. #else 
  151. #define SIGN_EXTEND_CHAR    /* As nothing.  */
  152. #endif /* not CHAR_UNSIGNED */
  153. #endif /* not SIGN_EXTEND_CHAR */
  154.  
  155. /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
  156.    use `alloca' instead of `malloc'.  This is because using malloc in
  157.    re_search* or re_match* could cause memory leaks when C-g is used in
  158.    Emacs; also, malloc is slower and causes storage fragmentation.  On
  159.    the other hand, malloc is more portable, and easier to debug.  
  160.    
  161.    Because we sometimes use alloca, some routines have to be macros,
  162.    not functions---alloca-allocated space disappears at the end of the
  163.    function it is called in.  */
  164. #ifdef REGEX_MALLOC
  165.  
  166. #define REGEX_ALLOCATE malloc
  167. #define REGEX_REALLOCATE(source, size) (realloc (source, size))
  168.  
  169. #else /* not REGEX_MALLOC  */
  170.  
  171. /* Emacs already defines alloca, sometimes.  */
  172. #ifndef alloca
  173.  
  174. /* Make alloca work the best possible way.  */
  175. #ifdef __GNUC__
  176. #define alloca __builtin_alloca
  177. #else /* not __GNUC__ */
  178. #ifdef sparc
  179. #include <alloca.h>
  180. #else /* not __GNUC__ or sparc */
  181. char *alloca ();
  182. #endif  /* not sparc */ 
  183. #endif  /* not __GNUC__ */
  184.  
  185. #endif /* not alloca */
  186.  
  187. /* Still not REGEX_MALLOC.  */
  188.  
  189. #define REGEX_ALLOCATE alloca
  190.  
  191. /* Requires a `char *destination' declared.  */
  192. #define REGEX_REALLOCATE(source, size)                    \
  193.   (destination = (char *) alloca (size),                \
  194.    bcopy (source, destination, size),                    \
  195.    destination)
  196.  
  197. #endif /* not REGEX_MALLOC */
  198.  
  199. /* (Re)Allocate N items of type T using malloc, or fail.  */
  200. #define TALLOC(n, t) (t *) malloc ((n) * sizeof (t))
  201. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  202.  
  203.  
  204. #define BYTEWIDTH 8 /* In bits.  */
  205.  
  206. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  207.  
  208. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  209. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  210.  
  211. /* These are the command codes that appear in compiled regular
  212.    expressions.  Some opcodes are followed by argument bytes.  A
  213.    command code can specify any interpretation whatsoever for its
  214.    arguments.  Zero bytes may appear in the compiled regular expression.
  215.  
  216.    The value of `exactn' is needed in search.c (search_buffer) in Emacs.
  217.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  218.    `exactn' we use here must also be 1.  */
  219.  
  220. typedef enum
  221. {
  222.   no_op = 0,
  223.  
  224.         /* Followed by one byte giving n, then by n literal bytes.  */
  225.   exactn = 1,
  226.  
  227.         /* Matches any (more or less) character.  */
  228.   anychar,
  229.  
  230.         /* Matches any one char belonging to specified set.  First
  231.            following byte is number of bitmap bytes.  Then come bytes
  232.            for a bitmap saying which chars are in.  Bits in each byte
  233.            are ordered low-bit-first.  A character is in the set if its
  234.            bit is 1.  A character too large to have a bit in the map is
  235.            automatically not in the set.  */
  236.   charset,
  237.  
  238.         /* Same parameters as charset, but match any character that is
  239.            not one of those specified.  */
  240.   charset_not,
  241.  
  242.         /* Start remembering the text that is matched, for storing in a
  243.            register.  Followed by one byte with the register number, in
  244.            the range 0 to one less than the pattern buffer's re_nsub
  245.            field.  Then followed by one byte with the number of groups
  246.            inner to this one.  (This last has to be part of the
  247.            start_memory only because we need it in the on_failure_jump
  248.            of re_match_2.)  */
  249.   start_memory,
  250.  
  251.         /* Stop remembering the text that is matched and store it in a
  252.            memory register.  Followed by one byte with the register
  253.            number, in the range 0 to one less than `re_nsub' in the
  254.            pattern buffer, and one byte with the number of inner groups,
  255.            just like `start_memory'.  (We need the number of inner
  256.            groups here because we don't have any easy way of finding the
  257.            corresponding start_memory when we're at a stop_memory.)  */
  258.   stop_memory,
  259.  
  260.         /* Match a duplicate of something remembered. Followed by one
  261.            byte containing the register number.  */
  262.   duplicate,
  263.  
  264.         /* Fail unless at beginning of line.  */
  265.   begline,
  266.  
  267.         /* Fail unless at end of line.  */
  268.   endline,
  269.  
  270.         /* Succeeds if at beginning of buffer (if emacs) or at beginning
  271.            of string to be matched (if not).  */
  272.   begbuf,
  273.  
  274.         /* Analogously, for end of buffer/string.  */
  275.   endbuf,
  276.  
  277.         /* Followed by two byte relative address to which to jump.  */
  278.   no_pop_jump, 
  279.  
  280.     /* Same as no_pop_jump, but marks the end of an alternative.  */
  281.   jump_past_next_alt,
  282.  
  283.         /* Followed by two-byte relative address of place to resume at
  284.            in case of failure.  */
  285.   on_failure_jump,
  286.     
  287.         /* Like on_failure_jump, but pushes a placeholder instead of the
  288.            current string position.  */
  289.   on_failure_keep_string_jump,
  290.   
  291.         /* Throw away latest failure point and then jump to following
  292.            two-byte relative address.  */
  293.   pop_failure_jump,
  294.  
  295.         /* Change to pop_failure_jump if know won't have to backtrack to
  296.            match; otherwise change to no_pop_jump.  This is used to jump
  297.            back to the beginning of a repeat.  If what follows this jump
  298.            clearly won't match what the repeat does, such that we can be
  299.            sure that there is no use backtracking out of repetitions
  300.            already matched, then we change it to a pop_failure_jump.
  301.            Followed by two-byte address.  */
  302.   maybe_pop_jump,
  303.  
  304.         /* Jump to following two-byte address, and push a dummy failure
  305.            point. This failure point will be thrown away if an attempt
  306.            is made to use it for a failure.  A `+' construct makes this
  307.            before the first repeat.  Also used as an intermediary kind
  308.            of jump when compiling an alternative.  */
  309.   dummy_failure_jump,
  310.  
  311.         /* Used like on_failure_jump except has to succeed n times; The
  312.            two-byte relative address following it is useless until then.
  313.            The address is followed by two more bytes containing n.  */
  314.   succeed_n,
  315.  
  316.         /* Similar to no_pop_jump, but jump n times only; also the
  317.            relative address following is in turn followed by yet two
  318.            more bytes containing n.  */
  319.   no_pop_jump_n,
  320.  
  321.         /* Set the following relative location (two bytes) to the
  322.            subsequent (two-byte) number.  */
  323.   set_number_at,
  324.  
  325.   wordchar,    /* Matches any word-constituent character.  */
  326.   notwordchar,    /* Matches any char that is not a word-constituent.  */
  327.  
  328.   wordbeg,    /* Succeeds if at word beginning.  */
  329.   wordend,    /* Succeeds if at word end.  */
  330.  
  331.   wordbound,    /* Succeeds if at a word boundary.  */
  332.   notwordbound    /* Succeeds if not at a word boundary.  */
  333.  
  334. #ifdef emacs
  335.   ,before_dot,    /* Succeeds if before point.  */
  336.   at_dot,    /* Succeeds if at point.  */
  337.   after_dot,    /* Succeeds if after point.  */
  338.  
  339.     /* Matches any character whose syntax is specified.  Followed by
  340.            a byte which contains a syntax code, e.g., Sword.  */
  341.   syntaxspec,
  342.  
  343.     /* Matches any character whose syntax is not that specified.  */
  344.   notsyntaxspec
  345. #endif /* emacs */
  346. } re_opcode_t;
  347.  
  348. /* Common operations on the compiled pattern.  */
  349.  
  350. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  351.  
  352. #define STORE_NUMBER(destination, number)                \
  353.   do {                                    \
  354.     (destination)[0] = (number) & 0377;                    \
  355.     (destination)[1] = (number) >> 8;                    \
  356.   } while (0)
  357.  
  358.  
  359. /* Same as STORE_NUMBER, except increment DESTINATION to
  360.    the byte after where the number is stored.  Therefore, DESTINATION
  361.    must be an lvalue.  */
  362.  
  363. #define STORE_NUMBER_AND_INCR(destination, number)            \
  364.   do {                                    \
  365.     STORE_NUMBER (destination, number);                    \
  366.     (destination) += 2;                            \
  367.   } while (0)
  368.  
  369.  
  370. /* Put into DESTINATION a number stored in two contingous bytes starting
  371.    at SOURCE.  */
  372.  
  373. #define EXTRACT_NUMBER(destination, source)                \
  374.   do {                                    \
  375.     (destination) = *(source) & 0377;                    \
  376.     (destination) += SIGN_EXTEND_CHAR (*(const char *)((source) + 1)) << 8;\
  377.   } while (0)
  378.  
  379. #ifdef DEBUG
  380. static int
  381. extract_number (source)
  382.     unsigned char *source;
  383. {
  384.   int answer = *source & 0377;
  385.   answer += (SIGN_EXTEND_CHAR (*(char *)((source) + 1))) << 8;
  386.   
  387.   return answer;
  388. }
  389. #endif
  390.  
  391.  
  392. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  393.    SOURCE must be an lvalue.  */
  394.  
  395. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  396.   do {                                    \
  397.     EXTRACT_NUMBER (destination, source);                \
  398.     (source) += 2;                             \
  399.   } while (0)
  400.  
  401. #ifdef DEBUG
  402. static void
  403. extract_number_and_incr (destination, source)
  404.     int *destination;
  405.     unsigned char **source;
  406.   *destination = extract_number (*source);
  407.   *source += 2;
  408. }
  409. #endif
  410.  
  411.  
  412. /* Is true if there is a first string and if PTR is pointing anywhere
  413.    inside it or just past the end.  */
  414.    
  415. #define IS_IN_FIRST_STRING(ptr)                     \
  416.   (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  417.  
  418. #ifdef DEBUG
  419.  
  420. extern void printchar ();
  421.  
  422. /* Print a compiled pattern buffer in human-readable form, starting at
  423.    the START pointer into it and ending just before the pointer END.  */
  424.  
  425. static void
  426. partial_compiled_pattern_printer (pbufp, start, end)
  427.     struct re_pattern_buffer *pbufp;
  428.     unsigned char *start;
  429.     unsigned char *end;
  430. {
  431.   
  432.   int mcnt, mcnt2;
  433.   unsigned char *p = start;
  434.   unsigned char *pend = end;
  435.  
  436.   if (start == NULL)
  437.     {
  438.       printf ("(null)\n");
  439.       return;
  440.     }
  441.     
  442.   /* This loop loops over pattern commands.  */
  443.   while (p < pend)
  444.     {
  445.       switch ((re_opcode_t) *p++)
  446.     {
  447.         case no_op:
  448.           printf ("/no_op");
  449.           break;
  450.  
  451.     case exactn:
  452.       mcnt = *p++;
  453.           printf ("/exactn/%d", mcnt);
  454.           do
  455.         {
  456.               putchar ('/');
  457.           printchar (*p++);
  458.             }
  459.           while (--mcnt);
  460.           break;
  461.  
  462.     case start_memory:
  463.           mcnt = *p++;
  464.           printf ("/start_memory/%d/%d", mcnt, *p++);
  465.           break;
  466.  
  467.     case stop_memory:
  468.           mcnt = *p++;
  469.       printf ("/stop_memory/%d/%d", mcnt, *p++);
  470.           break;
  471.  
  472.     case duplicate:
  473.       printf ("/duplicate/%d", *p++);
  474.       break;
  475.  
  476.     case anychar:
  477.       printf ("/anychar");
  478.       break;
  479.  
  480.     case charset:
  481.         case charset_not:
  482.           {
  483.             register int c;
  484.  
  485.             printf ("/charset%s/", *(p - 1) == charset_not ? "_not" : "");
  486.  
  487.             for (c = 0; p < pend && c < *p * BYTEWIDTH; c++)
  488.               {
  489.                 if (p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  490.                   printchar (c);
  491.               }
  492.         p += 1 + *p;
  493.         break;
  494.       }
  495.  
  496.     case begline:
  497.       printf ("/begline");
  498.           break;
  499.  
  500.     case endline:
  501.           printf ("/endline");
  502.           break;
  503.  
  504.     case on_failure_jump:
  505.           extract_number_and_incr (&mcnt, &p);
  506.         printf ("/on_failure_jump/0/%d", mcnt);
  507.           break;
  508.  
  509.     case on_failure_keep_string_jump:
  510.           extract_number_and_incr (&mcnt, &p);
  511.         printf ("/on_failure_keep_string_jump/0/%d", mcnt);
  512.           break;
  513.  
  514.     case dummy_failure_jump:
  515.           extract_number_and_incr (&mcnt, &p);
  516.         printf ("/dummy_failure_jump/0/%d", mcnt);
  517.           break;
  518.  
  519.         case maybe_pop_jump:
  520.           extract_number_and_incr (&mcnt, &p);
  521.         printf ("/maybe_pop_jump/0/%d", mcnt);
  522.       break;
  523.  
  524.         case pop_failure_jump:
  525.       extract_number_and_incr (&mcnt, &p);
  526.         printf ("/pop_failure_jump/0/%d", mcnt);
  527.       break;          
  528.           
  529.         case jump_past_next_alt:
  530.       extract_number_and_incr (&mcnt, &p);
  531.         printf ("/jump_past_next_alt/0/%d", mcnt);
  532.       break;          
  533.           
  534.         case no_pop_jump:
  535.       extract_number_and_incr (&mcnt, &p);
  536.         printf ("/no_pop_jump/0/%d", mcnt);
  537.       break;
  538.  
  539.         case succeed_n: 
  540.           extract_number_and_incr (&mcnt, &p);
  541.           extract_number_and_incr (&mcnt2, &p);
  542.        printf ("/succeed_n/0/%d/0/%d", mcnt, mcnt2);
  543.           break;
  544.         
  545.         case no_pop_jump_n: 
  546.           extract_number_and_incr (&mcnt, &p);
  547.           extract_number_and_incr (&mcnt2, &p);
  548.        printf ("/no_pop_jump_n/0/%d/0/%d", mcnt, mcnt2);
  549.           break;
  550.         
  551.         case set_number_at: 
  552.           extract_number_and_incr (&mcnt, &p);
  553.           extract_number_and_incr (&mcnt2, &p);
  554.        printf ("/set_number_at/0/%d/0/%d", mcnt, mcnt2);
  555.           break;
  556.         
  557.         case wordbound:
  558.       printf ("/wordbound");
  559.       break;
  560.  
  561.     case notwordbound:
  562.       printf ("/notwordbound");
  563.           break;
  564.  
  565.     case wordbeg:
  566.       printf ("/wordbeg");
  567.       break;
  568.           
  569.     case wordend:
  570.       printf ("/wordend");
  571.           
  572. #ifdef emacs
  573.     case before_dot:
  574.       printf ("/before_dot");
  575.           break;
  576.  
  577.     case at_dot:
  578.       printf ("/at_dot");
  579.           break;
  580.  
  581.     case after_dot:
  582.       printf ("/after_dot");
  583.           break;
  584.  
  585.     case wordchar:
  586.           printf ("/wordchar-emacs");
  587.       mcnt = (int) Sword;
  588.       break;
  589.  
  590.     case syntaxspec:
  591.           printf ("/syntaxspec");
  592.       mcnt = *p++;
  593.       printf ("/%d", mcnt);
  594.           break;
  595.       
  596.     case notwordchar:
  597.           printf ("/notwordchar-emacs");
  598.       mcnt = (int) Sword;
  599.       break;
  600.  
  601.     case notsyntaxspec:
  602.           printf ("/notsyntaxspec");
  603.       mcnt = *p++;
  604.       printf ("/%d", mcnt);
  605.       break;
  606. #else /* not emacs */
  607.     case wordchar:
  608.       printf ("/wordchar-notemacs");
  609.           break;
  610.       
  611.     case notwordchar:
  612.       printf ("/notwordchar-notemacs");
  613.           break;
  614. #endif /* not emacs */
  615.  
  616.     case begbuf:
  617.       printf ("/begbuf");
  618.           break;
  619.  
  620.     case endbuf:
  621.       printf ("/endbuf");
  622.           break;
  623.  
  624.         default:
  625.           printf ("?%d", *(p-1));
  626.     }
  627.     }
  628.   printf ("/\n");
  629. }
  630.  
  631. static void
  632. compiled_pattern_printer (pbufp)
  633.     struct re_pattern_buffer *pbufp;
  634. {
  635.   partial_compiled_pattern_printer (pbufp, pbufp->buffer, 
  636.                        pbufp->buffer + pbufp->used);
  637. }
  638.  
  639.  
  640. static void
  641. double_string_printer (where, string1, size1, string2, size2)
  642.     unsigned char *where;
  643.     unsigned char *string1;
  644.     unsigned char *string2;
  645.     int size1;
  646.     int size2;
  647. {
  648.   unsigned this_char;
  649.   
  650.   if (where == NULL)
  651.     printf ("(null)");
  652.   else
  653.     {
  654.       if (IS_IN_FIRST_STRING (where))
  655.         {
  656.           for (this_char = where - string1; this_char < size1; this_char++)
  657.             printchar (string1[this_char]);
  658.  
  659.           where = string2;    
  660.         }
  661.  
  662.       for (this_char = where - string2; this_char < size2; this_char++)
  663.         printchar (string2[this_char]);
  664.     }
  665. }
  666.  
  667. #endif /* DEBUG */
  668.  
  669. #ifdef DEBUG
  670.  
  671. /* It is useful to test things that must to be true when debugging.  */
  672. #include <assert.h>
  673.  
  674. static int debug = 0;
  675.  
  676. #define DEBUG_STATEMENT(e) e
  677. #define DEBUG_PRINT1(x) if (debug) printf (x)
  678. #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  679. #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  680. #define DEBUG_COMPILED_PATTERN_PRINTER(p, s, e)             \
  681.   if (debug) partial_compiled_pattern_printer (p, s, e)
  682. #define DEBUG_DOUBLE_STRING_PRINTER(w, s1, sz1, s2, sz2)        \
  683.   if (debug) double_string_printer (w, s1, sz1, s2, sz2)
  684.  
  685. #else /* not DEBUG */
  686.  
  687. #undef assert
  688. #define assert(e)
  689.  
  690. #define DEBUG_STATEMENT(e)
  691. #define DEBUG_PRINT1(x)
  692. #define DEBUG_PRINT2(x1, x2)
  693. #define DEBUG_PRINT3(x1, x2, x3)
  694. #define DEBUG_COMPILED_PATTERN_PRINTER(p, s, e)
  695. #define DEBUG_DOUBLE_STRING_PRINTER(w, s1, sz1, s2, sz2)
  696.  
  697. #endif /* not DEBUG */
  698.  
  699. typedef char boolean;
  700. #define false 0
  701. #define true 1
  702.  
  703. /* Set by re_set_syntax to the current regexp syntax to recognize.  Can
  704.    also be assigned to more or less arbitrarily.  Since we use this as a
  705.    collection of bits, declaring it unsigned maximizes portability.  */
  706. reg_syntax_t obscure_syntax = 0;
  707.  
  708.  
  709. /* Specify the precise syntax of regexps for compilation.  This provides
  710.    for compatibility for various utilities which historically have
  711.    different, incompatible syntaxes.
  712.  
  713.    The argument SYNTAX is a bit mask comprised of the various bits
  714.    defined in regex.h.  We return the old syntax.  */
  715.  
  716. reg_syntax_t
  717. re_set_syntax (syntax)
  718.     reg_syntax_t syntax;
  719. {
  720.   reg_syntax_t ret = obscure_syntax;
  721.   
  722.   obscure_syntax = syntax;
  723.   return ret;
  724. }
  725.  
  726. /* This table gives an error message for each of the error codes listed
  727.    in regex.h.  Obviously the order here has to be same as there.  */
  728.  
  729. static const char *re_error_msg[] =
  730.   { NULL,                    /* REG_NOERROR */
  731.     "No match",                    /* REG_NOMATCH */
  732.     "Invalid regular expression",        /* REG_BADPAT */
  733.     "Invalid collation character",        /* REG_ECOLLATE */
  734.     "Invalid character class name",        /* REG_ECTYPE */
  735.     "Trailing backslash",            /* REG_EESCAPE */
  736.     "Invalid back reference",            /* REG_ESUBREG */
  737.     "Unmatched [ or [^",            /* REG_EBRACK */
  738.     "Unmatched ( or \\(",            /* REG_EPAREN */
  739.     "Unmatched \\{",                /* REG_EBRACE */
  740.     "Invalid content of \\{\\}",        /* REG_BADBR */
  741.     "Invalid range end",            /* REG_ERANGE */
  742.     "Memory exhausted",                /* REG_ESPACE */
  743.     "Invalid preceding regular expression",    /* REG_BADRPT */
  744.     "Premature end of regular expression",    /* REG_EEND */
  745.     "Regular expression too big",        /* REG_ESIZE */
  746.     "Unmatched ) or \\)",            /* REG_ERPAREN */
  747.   };
  748.  
  749. /* Other subroutine declarations and macros for regex_compile.  */
  750.  
  751. static void store_jump (), insert_jump (), store_jump_n (),
  752.             insert_jump_n (), insert_op_2 ();
  753.  
  754. static boolean at_endline_op_p (), group_in_compile_stack ();
  755.  
  756. /* Fetch the next character in the uncompiled pattern---translating it 
  757.    if necessary.  Also cast from a signed character in the constant
  758.    string passed to us by the user to an unsigned char that we can use
  759.    as an array index (in, e.g., `translate').  */
  760. #define PATFETCH(c)                            \
  761.   do {if (p == pend) return REG_EEND;                    \
  762.     c = (unsigned char) *p++;                        \
  763.     if (translate) c = translate[c];                     \
  764.   } while (0)
  765.  
  766. /* Fetch the next character in the uncompiled pattern, with no
  767.    translation.  */
  768. #define PATFETCH_RAW(c)                            \
  769.   do {if (p == pend) return REG_EEND;                    \
  770.     c = (unsigned char) *p++;                         \
  771.   } while (0)
  772.  
  773. /* Go backwards one character in the pattern.  */
  774. #define PATUNFETCH p--
  775.  
  776.  
  777. /* If `translate' is non-null, return translate[D], else just D.  We
  778.    cast the subscript to translate because some data is declared as
  779.    `char *', to avoid warnings when a string constant is passed.  But
  780.    when we use a character as a subscript we must make it unsigned.  */
  781. #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
  782.  
  783.  
  784. /* Macros for outputting the compiled pattern into `buffer'.  */
  785.  
  786. /* If the buffer isn't allocated when it comes in, use this.  */
  787. #define INIT_BUF_SIZE  32
  788.  
  789. /* Make sure we have at least N more bytes of space in buffer.  */
  790. #define GET_BUFFER_SPACE(n)                        \
  791.   {                                        \
  792.     while (b - bufp->buffer + (n) > bufp->allocated)            \
  793.       EXTEND_BUFFER ();                            \
  794.   }
  795.  
  796. /* Make sure we have one more byte of buffer space and then add C to it.  */
  797. #define PAT_PUSH(c)                            \
  798.   do {                                    \
  799.     GET_BUFFER_SPACE (1);                        \
  800.     *b++ = (unsigned char) (c);                        \
  801.   } while (0)
  802.  
  803.  
  804. /* Make sure we have two more bytes of buffer space and then add C1 and 
  805.    C2 to it.  */
  806. #define PAT_PUSH_2(c1, c2)                        \
  807.   do {                                    \
  808.     GET_BUFFER_SPACE (2);                        \
  809.     *b++ = (unsigned char) (c1);                    \
  810.     *b++ = (unsigned char) (c2);                    \
  811.   } while (0)
  812.  
  813.  
  814. /* Make sure we have two more bytes of buffer space and then add C1, C2
  815.    and C3 to it.  */
  816. #define PAT_PUSH_3(c1, c2, c3)                        \
  817.   do {                                    \
  818.     GET_BUFFER_SPACE (3);                        \
  819.     *b++ = (unsigned char) (c1);                    \
  820.     *b++ = (unsigned char) (c2);                    \
  821.     *b++ = (unsigned char) (c3);                    \
  822.   } while (0)
  823.  
  824. /* This is not an arbitrary limit: the arguments to the opcodes which
  825.    represent offsets into the pattern are two bytes long.  So if 2^16
  826.    bytes turns out to be too small, many things would have to change.  */
  827. #define MAX_BUF_SIZE (1L << 16)
  828.  
  829. /* Extend the buffer by twice its current size via realloc and
  830.    reset the pointers that pointed into the old block to point to the
  831.    correct places in the new one.  If extending the buffer results in it
  832.    being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
  833. #define EXTEND_BUFFER()                            \
  834.   do {                                     \
  835.     unsigned char *old_buffer = bufp->buffer;                \
  836.     if (bufp->allocated == MAX_BUF_SIZE)                 \
  837.       return REG_ESIZE;                            \
  838.     bufp->allocated <<= 1;                        \
  839.     if (bufp->allocated > MAX_BUF_SIZE)                    \
  840.       bufp->allocated = MAX_BUF_SIZE;                     \
  841.     bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
  842.     if (bufp->buffer == NULL)                        \
  843.       return REG_ESPACE;                        \
  844.     /* If the buffer moved, move all the pointers into it.  */        \
  845.     if (old_buffer != bufp->buffer)                    \
  846.       {                                    \
  847.         b = (b - old_buffer) + bufp->buffer;                \
  848.         begalt = (begalt - old_buffer) + bufp->buffer;            \
  849.         if (fixup_alt_jump)                        \
  850.           fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  851.         if (laststart)                            \
  852.           laststart = (laststart - old_buffer) + bufp->buffer;        \
  853.         if (pending_exact)                        \
  854.           pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  855.       }                                    \
  856.   } while (0)
  857.  
  858.  
  859. /* Since we have one byte reserved for the register number argument to
  860.    {start,stop}_memory, the maximum number of groups we can report
  861.    things about is what fits in that byte.  */
  862. typedef unsigned char regnum_t;
  863. #define MAX_REGNUM ((regnum_t) ((1 << BYTEWIDTH) - 1))
  864.  
  865.  
  866. /* Macros for the compile stack.  */
  867.  
  868. /* This type needs to be able to hold values from 0 to MAX_BUF_SIZE - 1.  */
  869. typedef short pattern_offset_t;
  870.  
  871. typedef struct
  872. {
  873.   pattern_offset_t begalt_offset;
  874.   pattern_offset_t fixup_alt_jump;
  875.   pattern_offset_t inner_group_offset;
  876.   pattern_offset_t laststart_offset;  
  877.   regnum_t regnum;
  878. } compile_stack_elt_t;
  879.  
  880.  
  881. typedef struct
  882. {
  883.   compile_stack_elt_t *stack;
  884.   unsigned size;
  885.   unsigned avail;            /* Offset of next open position.  */
  886. } compile_stack_type;
  887.  
  888.  
  889. #define INIT_COMPILE_STACK_SIZE 32
  890.  
  891. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  892. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  893.  
  894. /* The next available element.  */
  895. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  896.  
  897.  
  898. /* Set the bit for character C in a list.  */
  899. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  900.  
  901.  
  902. /* Get the next unsigned number in the uncompiled pattern.  */
  903. #define GET_UNSIGNED_NUMBER(num)                     \
  904.   { if (p != pend)                            \
  905.      {                                    \
  906.        PATFETCH (c);                             \
  907.        while (isdigit (c))                         \
  908.          {                                 \
  909.            if (num < 0)                            \
  910.               num = 0;                            \
  911.            num = num * 10 + c - '0';                     \
  912.            if (p == pend)                         \
  913.               break;                             \
  914.            PATFETCH (c);                        \
  915.          }                                 \
  916.        }                                 \
  917.     }        
  918.  
  919.  
  920. /* Read the endpoint of a range from the uncompiled pattern and set the
  921.    corresponding bits in the compiled pattern.  */
  922.  
  923. #define DO_RANGE                            \
  924.   {                                    \
  925.     char end;                                \
  926.     char this_char = p[-2];                        \
  927.                                                                            \
  928.     if (p == pend)                            \
  929.       return REG_ERANGE;                        \
  930.     PATFETCH (end);                            \
  931.     if (syntax & RE_NO_EMPTY_RANGES && this_char > end)        \
  932.       return REG_ERANGE;                        \
  933.     while (this_char <= end)                        \
  934.       {                                    \
  935.         SET_LIST_BIT (TRANSLATE (this_char));                \
  936.         this_char++;                            \
  937.       }                                    \
  938.     }
  939.  
  940.  
  941. #define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
  942.  
  943. #define IS_CHAR_CLASS(string)                        \
  944.    (STREQ (string, "alpha") || STREQ (string, "upper")            \
  945.     || STREQ (string, "lower") || STREQ (string, "digit")        \
  946.     || STREQ (string, "alnum") || STREQ (string, "xdigit")        \
  947.     || STREQ (string, "space") || STREQ (string, "print")        \
  948.     || STREQ (string, "punct") || STREQ (string, "graph")        \
  949.     || STREQ (string, "cntrl") || STREQ (string, "blank"))
  950.  
  951.  
  952. /* regex_compile compiles PATTERN (of length SIZE) according to SYNTAX.
  953.    Returns one of error codes defined in regex.h, or zero for success.
  954.  
  955.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  956.    fields are set in BUFP on entry.
  957.  
  958.    If it succeeds, results are put in BUFP (if it returns an error, the
  959.    contents of BUFP are undefined):
  960.      `buffer' is the compiled pattern;
  961.      `syntax' is set to SYNTAX;
  962.      `used' is set to the length of the compiled pattern;
  963.      `fastmap_accurate' is set to zero;
  964.      `re_nsub' is set to the number of groups in PATTERN;
  965.      `not_bol' and `not_eol' are set to zero.
  966.    
  967.    The `fastmap' and `newline_anchor' fields are neither
  968.    examined nor set.  */
  969.  
  970. static reg_errcode_t
  971. regex_compile (pattern, size, syntax, bufp)
  972.      const char *pattern;
  973.      int size;
  974.      reg_syntax_t syntax;
  975.      struct re_pattern_buffer *bufp;
  976. {
  977.   register unsigned char c, c1;
  978.   const char *p1;
  979.  
  980.   /* Points to the end of the buffer, where we should append.  */
  981.   register unsigned char *b;
  982.   
  983.   /* Points to the current (ending) position in the pattern.  */
  984.   const char *p = pattern;
  985.   const char *pend = pattern + size;
  986.   
  987.   /* How to translate the characters in the pattern.  */
  988.   char *translate = bufp->translate;
  989.  
  990.   /* Address of the count-byte of the most recently inserted `exactn'
  991.      command.  This makes it possible to tell if a new exact-match
  992.      character can be added to that command or if the character requires
  993.      a new `exactn' command.  */
  994.   unsigned char *pending_exact = 0;
  995.  
  996.   /* Address of start of the most recently finished expression.
  997.      This tells, e.g., postfix * where to find the start of its
  998.      operand.  Reset at the beginning of groups and alternatives.  */
  999.   unsigned char *laststart = 0;
  1000.  
  1001.   /* Place in the uncompiled pattern (i.e., the {) to
  1002.      which to go back if the interval is invalid.  */
  1003.   const char *beg_interval; /* The `{'.  */
  1004.   const char *following_left_brace;
  1005.  
  1006.   /* Address of beginning of regexp, or inside of last group.  */
  1007.   unsigned char *begalt;
  1008.   
  1009.   /* Address of the place where a forward jump should go to the end of
  1010.      the containing expression.  Each alternative of an `or'---except the
  1011.      last---ends with a forward jump of this sort.  */
  1012.   unsigned char *fixup_alt_jump = 0;
  1013.  
  1014.   /* Counts open-groups as they are encountered.  Remembered for the
  1015.      matching close-group on the compile stack, so the same register
  1016.      number is put in the stop_memory as the start_memory.  The type
  1017.      here is determined by MAX_REGNUM.  */
  1018.   regnum_t regnum = 0;
  1019.  
  1020.   /* Keeps track of unclosed groups.  */
  1021.   compile_stack_type compile_stack;
  1022.  
  1023. #ifdef DEBUG
  1024.   DEBUG_PRINT1 ("\nCompiling pattern: ");
  1025.   if (debug)
  1026.     {
  1027.       unsigned debug_count;
  1028.       
  1029.       for (debug_count = 0; debug_count < size; debug_count++)
  1030.         printchar (pattern[debug_count]);
  1031.         
  1032.       DEBUG_PRINT1 ("\n");
  1033.     }
  1034. #endif /* DEBUG */
  1035.  
  1036.   /* Initialize the compile stack.  */
  1037.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1038.   if (compile_stack.stack == NULL)
  1039.     return REG_ESPACE;
  1040.  
  1041.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1042.   compile_stack.avail = 0;
  1043.  
  1044.   /* Initialize the pattern buffer.  */
  1045.   bufp->syntax = syntax;
  1046.   bufp->fastmap_accurate = 0;
  1047.   bufp->not_bol = bufp->not_eol = 0;
  1048.  
  1049.   /* Set `used' to zero, so that if we return an error, the pattern
  1050.      printer (for debugging) will think there's no pattern.  We reset it
  1051.      at the end.  */
  1052.   bufp->used = 0;
  1053.   
  1054.   /* Always count groups, whether or not bufp->no_sub is set.  */
  1055.   bufp->re_nsub = 0;                
  1056.  
  1057. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1058.   /* Initialize the syntax table.  */
  1059.    init_syntax_once ();
  1060. #endif
  1061.  
  1062.   if (bufp->allocated == 0)
  1063.     {
  1064.       if (bufp->buffer)
  1065.     { /* EXTEND_BUFFER loses when bufp->allocated is 0.  This loses if
  1066.              buffer's address is bogus, but that is the user's
  1067.              responsibility.  */
  1068.           RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1069.         }
  1070.       else
  1071.         { /* Caller did not allocate a buffer.  Do it for them.  */
  1072.           bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1073.         }
  1074.       if (!bufp->buffer) return REG_ESPACE;
  1075.  
  1076.       bufp->allocated = INIT_BUF_SIZE;
  1077.     }
  1078.  
  1079.   begalt = b = bufp->buffer;
  1080.  
  1081.   /* Loop through the uncompiled pattern until we're at the end.  */
  1082.   while (p != pend)
  1083.     {
  1084.       PATFETCH (c);
  1085.  
  1086.       switch (c)
  1087.         {
  1088.         /* ^ matches the empty string at the beginning of a string (or
  1089.            possibly a line).  If RE_CONTEXT_INDEP_ANCHORS is set, ^ is
  1090.            always an operator (and foo^bar is unmatchable).  If that bit
  1091.            isn't set, it's an operator only at the beginning of the
  1092.            pattern or after an alternation or open-group operator, or,
  1093.            if RE_NEWLINE_ORDINARY is not set, after a newline (except it
  1094.            can be preceded by other operators that match the empty
  1095.            string); otherwise, it's a normal character.  */
  1096.         case '^':
  1097.           {
  1098.             if (   /* If at start of (sub)pattern, it's an operator.  */
  1099.                    laststart == 0
  1100.                    /* If context independent, it's an operator.  */
  1101.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1102.                    /* If after a newline, might be an operator.  (Since
  1103.                       laststart is nonzero here, we know we have at
  1104.                       least one byte before the ^.)  */
  1105.                 || (!(syntax & RE_NEWLINE_ORDINARY) && p[-2] == '\n'))
  1106.               PAT_PUSH (begline);
  1107.             else
  1108.               goto normal_char;
  1109.           }
  1110.           break;
  1111.  
  1112.  
  1113.     /* $ matches the empty string following the end of the string (or
  1114.            possibly a line).  It follows rules dual to those for ^.  */
  1115.         case '$':
  1116.           {
  1117.             if (   /* If at end of pattern, it's an operator.  */
  1118.                    p == pend 
  1119.                    /* If context independent, it's an operator.  */
  1120.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1121.                    /* Otherwise, depends on what's next.  */
  1122.                 || at_endline_op_p (p, pend, syntax))
  1123.                PAT_PUSH (endline);
  1124.              else
  1125.                goto normal_char;
  1126.            }
  1127.            break;
  1128.  
  1129.  
  1130.     case '+':
  1131.         case '?':
  1132.           if ((syntax & RE_BK_PLUS_QM)
  1133.               || (syntax & RE_LIMITED_OPS))
  1134.             goto normal_char;
  1135.         handle_plus:
  1136.         case '*':
  1137.           /* If there is no previous pattern... */
  1138.           if (!laststart)
  1139.             {
  1140.               if (syntax & RE_CONTEXT_INVALID_OPS)
  1141.                 return REG_BADRPT;
  1142.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1143.                 goto normal_char;
  1144.             }
  1145.  
  1146.           {
  1147.             /* Are we optimizing this jump?  */
  1148.             boolean keep_string_p = false;
  1149.             
  1150.             /* 1 means zero (many) matches is allowed.  */
  1151.             char zero_times_ok = 0, many_times_ok = 0;
  1152.  
  1153.             /* If there is a sequence of repetition chars, collapse it
  1154.                down to just one (the right one).  We can't combine
  1155.                interval operators with these because of, e.g., `a{2}*',
  1156.                which should only match an even number of `a's.  */
  1157.  
  1158.             for (;;)
  1159.               {
  1160.                 zero_times_ok |= c != '+';
  1161.                 many_times_ok |= c != '?';
  1162.  
  1163.                 if (p == pend)
  1164.                   break;
  1165.  
  1166.                 PATFETCH (c);
  1167.  
  1168.                 if (c == '*'
  1169.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1170.                   ;
  1171.  
  1172.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  1173.                   {
  1174.                     if (p == pend) return REG_EESCAPE;
  1175.  
  1176.                     PATFETCH (c1);
  1177.                     if (!(c1 == '+' || c1 == '?'))
  1178.                       {
  1179.                         PATUNFETCH;
  1180.                         PATUNFETCH;
  1181.                         break;
  1182.                       }
  1183.  
  1184.                     c = c1;
  1185.                   }
  1186.                 else
  1187.                   {
  1188.                     PATUNFETCH;
  1189.                     break;
  1190.                   }
  1191.  
  1192.                 /* If we get here, we found another repeat character.  */
  1193.                }
  1194.  
  1195.             /* Star, etc. applied to an empty pattern is equivalent
  1196.                to an empty pattern.  */
  1197.             if (!laststart)  
  1198.               break;
  1199.  
  1200.             /* Now we know whether or not zero matches is allowed
  1201.                and also whether or not two or more matches is allowed.  */
  1202.             if (many_times_ok)
  1203.               { /* More than one repetition is allowed, so put in at the
  1204.                    end a backward relative jump from `b' to before the next
  1205.                    jump we're going to put in below (which jumps from
  1206.                    laststart to after this jump).  
  1207.  
  1208.                    But if we are at the `*' in the exact sequence `.*\n',
  1209.                    insert an unconditional jump backwards to the .,
  1210.                    instead of the beginning of the loop.  This way we only
  1211.                    push a failure point once, instead of every time
  1212.                    through the loop.  */
  1213.                 assert (p - 1 > pattern);
  1214.  
  1215.                 /* Get the space for the jump.  */
  1216.                 GET_BUFFER_SPACE (3);
  1217.  
  1218.                 /* We know we are not at the first character of the pattern,
  1219.                    because laststart was nonzero.  And we've already
  1220.                    incremented `p', by the way, to be the character after
  1221.                    the `*'.  Do we have to do something analogous here
  1222.                    for null bytes, because of RE_DOT_NOT_NULL?  */
  1223.                 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  1224.                     && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  1225.                     && !(syntax & RE_DOT_NEWLINE))
  1226.                   { /* We have .*\n.  */
  1227.                     store_jump (b, no_pop_jump, laststart);
  1228.                     keep_string_p = true;
  1229.                   }
  1230.                 else
  1231.                   /* Anything else.  */
  1232.                   store_jump (b, maybe_pop_jump, laststart - 3);
  1233.  
  1234.                 /* We've added more stuff to the buffer.  */
  1235.                 b += 3;
  1236.               }
  1237.  
  1238.             /* On failure, jump from laststart to b + 3, which will be the
  1239.                end of the buffer after this jump is inserted.  */
  1240.             GET_BUFFER_SPACE (3);
  1241.             insert_jump (keep_string_p ? on_failure_keep_string_jump
  1242.                                        : on_failure_jump,
  1243.                          laststart, b + 3, b);
  1244.             pending_exact = 0;
  1245.             b += 3;
  1246.  
  1247.             if (!zero_times_ok)
  1248.               {
  1249.                 /* At least one repetition is required, so insert a
  1250.                    dummy_failure before the initial on_failure_jump
  1251.                    instruction of the loop. This effects a skip over that
  1252.                    instruction the first time we hit that loop.  */
  1253.                 GET_BUFFER_SPACE (3);
  1254.                 insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  1255.                 b += 3;
  1256.               }
  1257.             }
  1258.       break;
  1259.  
  1260.  
  1261.     case '.':
  1262.           laststart = b;
  1263.           PAT_PUSH (anychar);
  1264.           break;
  1265.  
  1266.  
  1267.         case '[':
  1268.           {
  1269.             boolean just_had_a_char_class = false;
  1270.  
  1271.             if (p == pend) return REG_EBRACK;
  1272.  
  1273.             /* Ensure that we have enough space to push an entire
  1274.                charset: the opcode, the byte count, and the bitmap.  */
  1275.             while (b - bufp->buffer + 2 + (1 << BYTEWIDTH) / BYTEWIDTH
  1276.                    > bufp->allocated)
  1277.               EXTEND_BUFFER ();
  1278.  
  1279.             laststart = b;
  1280.  
  1281.             PAT_PUSH (*p == '^' ? charset_not : charset); 
  1282.             if (*p == '^')
  1283.               p++;
  1284.  
  1285.             /* Remember the first position in the bracket expression.  */
  1286.             p1 = p;
  1287.  
  1288.             /* Push the number of bytes in the bitmap.  */
  1289.             PAT_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1290.  
  1291.             /* Clear the whole map.  */
  1292.             bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1293.  
  1294.             /* charset_not matches newline according to a syntax bit.  */
  1295.             if ((re_opcode_t) b[-2] == charset_not
  1296.                 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1297.               SET_LIST_BIT ('\n');
  1298.  
  1299.             /* Read in characters and ranges, setting map bits.  */
  1300.             for (;;)
  1301.               {
  1302.                 if (p == pend) return REG_EBRACK;
  1303.  
  1304.                 PATFETCH (c);
  1305.  
  1306.                 /* \ might escape characters inside [...] and [^...].  */
  1307.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1308.                   {
  1309.                     if (p == pend) return REG_EESCAPE;
  1310.  
  1311.                     PATFETCH (c1);
  1312.                     SET_LIST_BIT (c1);
  1313.                     continue;
  1314.                   }
  1315.  
  1316.                 /* Could be the end of the bracket expression.  If it's
  1317.                    not (i.e., when the bracket expression is `[]' so
  1318.                    far), the ']' character bit gets set way below.  */
  1319.                 if (c == ']' && p != p1 + 1)
  1320.                   break;
  1321.  
  1322.                 /* Look ahead to see if it's a range when the last thing
  1323.                    was a character class.  */
  1324.                 if (just_had_a_char_class && c == '-' && *p != ']')
  1325.                   return REG_ERANGE;
  1326.  
  1327.                 /* Look ahead to see if it's a range when the last thing
  1328.                    was a character: if this is a hyphen not at the
  1329.                    beginning or the end of a list, then it's the range
  1330.                    operator.  */
  1331.                 if (c == '-' 
  1332.                     && !(p - 2 >= pattern && p[-2] == '[') 
  1333.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  1334.                     && *p != ']')
  1335.                   {
  1336.                     DO_RANGE;
  1337.                   }
  1338.  
  1339.                 else if (p[0] == '-' && p[1] != ']')
  1340.                   { /* This handles ranges made up of characters only.  */
  1341.                     PATFETCH (c1);        /* The `-'.  */
  1342.                     DO_RANGE;
  1343.                   }
  1344.  
  1345.                 /* See if we're at the beginning of a possible character
  1346.                    class.  */
  1347.  
  1348.                 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1349.                   { /* Leave room for the null.  */
  1350.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  1351.  
  1352.                     PATFETCH (c);
  1353.                     c1 = 0;
  1354.  
  1355.                     /* If pattern is `[[:'.  */
  1356.                     if (p == pend) return REG_EBRACK;
  1357.  
  1358.                     for (;;)
  1359.                       {
  1360.                         PATFETCH (c);
  1361.                         if (c == ':' || c == ']' || p == pend
  1362.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  1363.                           break;
  1364.                         str[c1++] = c;
  1365.                       }
  1366.                     str[c1] = '\0';
  1367.  
  1368.                     /* If isn't a word bracketed by `[:' and:`]':
  1369.                        undo the ending character, the letters, and leave 
  1370.                        the leading `:' and `[' (but set bits for them).  */
  1371.                     if (c == ':' && *p == ']')
  1372.                       {
  1373.                         int ch;
  1374.                         boolean is_alnum = STREQ (str, "alnum");
  1375.                         boolean is_alpha = STREQ (str, "alpha");
  1376.                         boolean is_blank = STREQ (str, "blank");
  1377.                         boolean is_cntrl = STREQ (str, "cntrl");
  1378.                         boolean is_digit = STREQ (str, "digit");
  1379.                         boolean is_graph = STREQ (str, "graph");
  1380.                         boolean is_lower = STREQ (str, "lower");
  1381.                         boolean is_print = STREQ (str, "print");
  1382.                         boolean is_punct = STREQ (str, "punct");
  1383.                         boolean is_space = STREQ (str, "space");
  1384.                         boolean is_upper = STREQ (str, "upper");
  1385.                         boolean is_xdigit = STREQ (str, "xdigit");
  1386.                         
  1387.                         if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  1388.  
  1389.                         /* Throw away the ] at the end of the character
  1390.                            class.  */
  1391.                         PATFETCH (c);                    
  1392.  
  1393.                         if (p == pend) return REG_EBRACK;
  1394.  
  1395.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1396.                           {
  1397.                             if (   (is_alnum  && isalnum (ch))
  1398.                                 || (is_alpha  && isalpha (ch))
  1399.                                 || (is_blank  && isblank (ch))
  1400.                                 || (is_cntrl  && iscntrl (ch))
  1401.                                 || (is_digit  && isdigit (ch))
  1402.                                 || (is_graph  && isgraph (ch))
  1403.                                 || (is_lower  && islower (ch))
  1404.                                 || (is_print  && isprint (ch))
  1405.                                 || (is_punct  && ispunct (ch))
  1406.                                 || (is_space  && isspace (ch))
  1407.                                 || (is_upper  && isupper (ch))
  1408.                                 || (is_xdigit && isxdigit (ch)))
  1409.                             SET_LIST_BIT (ch);
  1410.                           }
  1411.                         just_had_a_char_class = true;
  1412.                       }
  1413.                     else
  1414.                       {
  1415.                         c1++;
  1416.                         while (c1--)    
  1417.                           PATUNFETCH;
  1418.                         SET_LIST_BIT ('[');
  1419.                         SET_LIST_BIT (':');
  1420.                         just_had_a_char_class = false;
  1421.                       }
  1422.                   }
  1423.                 else
  1424.                   {
  1425.                     just_had_a_char_class = false;
  1426.                     SET_LIST_BIT (c);
  1427.                   }
  1428.               }
  1429.  
  1430.             /* Discard any (non)matching list bytes that are all 0 at the
  1431.                end of the map.  Decrease the map-length byte too.  */
  1432.             while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  1433.               b[-1]--; 
  1434.             b += b[-1];
  1435.           }
  1436.           break;
  1437.  
  1438.  
  1439.     case '(':
  1440.           if (syntax & RE_NO_BK_PARENS)
  1441.             goto handle_open;
  1442.           else
  1443.             goto normal_char;
  1444.  
  1445.  
  1446.         case ')':
  1447.           if (syntax & RE_NO_BK_PARENS)
  1448.             goto handle_close;
  1449.           else
  1450.             goto normal_char;
  1451.  
  1452.  
  1453.         case '\n':
  1454.           if (syntax & RE_NEWLINE_ALT)
  1455.             goto handle_bar;
  1456.           else
  1457.             goto normal_char;
  1458.  
  1459.  
  1460.     case '|':
  1461.           if (syntax & RE_NO_BK_VBAR)
  1462.             goto handle_bar;
  1463.           else
  1464.             goto normal_char;
  1465.  
  1466.  
  1467.         case '{':
  1468.            if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  1469.              goto handle_interval;
  1470.            else
  1471.              goto normal_char;
  1472.  
  1473.  
  1474.         case '\\':
  1475.           if (p == pend) return REG_EESCAPE;
  1476.  
  1477.           /* Do not translate the character after the \, so that we can
  1478.              distinguish, e.g., \B from \b, even if we normally would
  1479.              translate, e.g., B to b.  */
  1480.           PATFETCH_RAW (c);
  1481.  
  1482.           switch (c)
  1483.             {
  1484.             case '(':
  1485.               if (syntax & RE_NO_BK_PARENS)
  1486.                 goto normal_backslash;
  1487.             handle_open:
  1488.               if (syntax & RE_NO_EMPTY_GROUPS)
  1489.                 {
  1490.                   p1 = p;
  1491.                   if (!(syntax & RE_NO_BK_PARENS) && *p1 == '\\') p1++;
  1492.  
  1493.                   /* If found an empty group...  */
  1494.                   if (*p1 == ')') return REG_BADPAT;
  1495.                 }
  1496.  
  1497.               bufp->re_nsub++;
  1498.               regnum++;
  1499.  
  1500.               if (COMPILE_STACK_FULL)
  1501.                 { 
  1502.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  1503.                             compile_stack_elt_t);
  1504.                   if (compile_stack.stack == NULL) return REG_ESPACE;
  1505.  
  1506.                   compile_stack.size <<= 1;
  1507.                 }
  1508.  
  1509.               /* These are the values to restore when we hit end of this
  1510.                  group.  They are all relative offsets, so that if the
  1511.                  whole pattern moves because of realloc, they will still
  1512.                  be valid.  */
  1513.               COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  1514.               COMPILE_STACK_TOP.fixup_alt_jump 
  1515.                 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  1516.               COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  1517.               COMPILE_STACK_TOP.regnum = regnum;
  1518.  
  1519.               /* We will eventually replace the 0 with the number of
  1520.                  groups inner to this one.  */
  1521.               if (regnum <= MAX_REGNUM)
  1522.                 {
  1523.                   COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  1524.                   PAT_PUSH_3 (start_memory, regnum, 0);
  1525.                 }
  1526.                 
  1527.               compile_stack.avail++;
  1528.  
  1529.               fixup_alt_jump = 0;
  1530.               laststart = 0;
  1531.               begalt = b;
  1532.               break;
  1533.  
  1534.  
  1535.             case ')':
  1536.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  1537.  
  1538.               if (COMPILE_STACK_EMPTY)
  1539.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  1540.                   goto normal_backslash;
  1541.                 else
  1542.                   return REG_ERPAREN;
  1543.  
  1544.             handle_close:
  1545.               if (fixup_alt_jump)
  1546.                 store_jump (fixup_alt_jump, jump_past_next_alt, b);
  1547.  
  1548.               /* See similar code for backslashed left paren above.  */
  1549.  
  1550.               if (COMPILE_STACK_EMPTY)
  1551.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  1552.                   goto normal_char;
  1553.                 else
  1554.                   return REG_ERPAREN;
  1555.  
  1556.               /* Since we just checked for an empty stack above, this
  1557.                  ``can't happen''.  */
  1558.               assert (compile_stack.avail != 0);
  1559.               {
  1560.                 /* We don't just want to restore into `regnum', because
  1561.                    later groups should continue to be numbered higher,
  1562.                    as in `(ab)c(de)' -- the second group is #2.  */
  1563.                 regnum_t this_group_regnum;
  1564.  
  1565.                 compile_stack.avail--;        
  1566.                 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  1567.                 fixup_alt_jump
  1568.                   = COMPILE_STACK_TOP.fixup_alt_jump
  1569.                     ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1 
  1570.                     : 0;
  1571.                 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  1572.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  1573.  
  1574.                 /* We're at the end of the group, so now we know how many
  1575.                    groups were inside this one.  */
  1576.                 if (this_group_regnum <= MAX_REGNUM)
  1577.                   {
  1578.                     unsigned char *inner_group_loc
  1579.                       = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  1580.                     
  1581.                     *inner_group_loc = regnum - this_group_regnum;
  1582.                     PAT_PUSH_3 (stop_memory, this_group_regnum,
  1583.                                 regnum - this_group_regnum);
  1584.                   }
  1585.               }
  1586.               break;
  1587.  
  1588.  
  1589.             case '|':                    /* `\|'.  */
  1590.               if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  1591.                 goto normal_backslash;
  1592.             handle_bar:
  1593.               if (syntax & RE_LIMITED_OPS)
  1594.                 goto normal_char;
  1595.  
  1596.               /* Disallow empty alternatives if RE_NO_EMPTY_ALTS is set.
  1597.                  Caveat: can't detect if the vbar is followed by a
  1598.                  trailing '$' yet, unless it's the last thing in a
  1599.                  pattern; the routine for verifying endlines has to do
  1600.                  the rest.  */
  1601.               if ((syntax & RE_NO_EMPTY_ALTS)
  1602.                   && (!laststart  ||  p == pend 
  1603.                       || (*p == '$' && p + 1 == pend)
  1604.                       || ((syntax & RE_NO_BK_PARENS)
  1605.                            ? (p < pend  &&  *p == ')')
  1606.                            : (p + 1 < pend && p[0] == '\\' && p[1] == ')'))))
  1607.                 return REG_BADPAT;
  1608.  
  1609.               /* Insert before the previous alternative a jump which
  1610.                  jumps to this alternative if the former fails.  */
  1611.               GET_BUFFER_SPACE (3);
  1612.               insert_jump (on_failure_jump, begalt, b + 6, b);
  1613.               pending_exact = 0;
  1614.               b += 3;
  1615.  
  1616.               /* The alternative before this one has a jump after it
  1617.                  which gets executed if it gets matched.  Adjust that
  1618.                  jump so it will jump to this alternative's analogous
  1619.                  jump (put in below, which in turn will jump to the next
  1620.                  (if any) alternative's such jump, etc.).  The last such
  1621.                  jump jumps to the correct final destination.  A picture:
  1622.                           _____ _____ 
  1623.                           |   | |   |   
  1624.                           |   v |   v 
  1625.                          a | b   | c   
  1626.  
  1627.                  If we are at `b,' then fixup_alt_jump right now points to a
  1628.                  three-byte space after `a.'  We'll put in the jump, set
  1629.                  fixup_alt_jump to right after `b,' and leave behind three
  1630.                  bytes which we'll fill in when we get to after `c.'  */
  1631.  
  1632.               if (fixup_alt_jump)
  1633.                 store_jump (fixup_alt_jump, jump_past_next_alt, b);
  1634.  
  1635.               /* Mark and leave space for a jump after this alternative,
  1636.                  to be filled in later either by next alternative or
  1637.                  when know we're at the end of a series of alternatives.  */
  1638.               fixup_alt_jump = b;
  1639.               GET_BUFFER_SPACE (3);
  1640.               b += 3;
  1641.  
  1642.               laststart = 0;
  1643.               begalt = b;
  1644.               break;
  1645.  
  1646.  
  1647.             case '{': 
  1648.               /* If \{ is a literal.  */
  1649.               if (!(syntax & RE_INTERVALS)
  1650.                      /* If we're at `\{' and it's not the open-interval 
  1651.                         operator.  */
  1652.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1653.                   || (p - 2 == pattern  &&  p == pend))
  1654.                 goto normal_backslash;
  1655.  
  1656.             handle_interval:
  1657.               {
  1658.                 /* If got here, then intervals must be allowed.  */
  1659.  
  1660.                 /* For intervals, at least (most) this many matches must
  1661.                    be made.  */
  1662.                 int lower_bound = -1, upper_bound = -1;
  1663.  
  1664.                 beg_interval = p - 1;         /* The `{'.  */
  1665.             following_left_brace = NULL;
  1666.  
  1667.                 if (p == pend)
  1668.                   {
  1669.                     if (syntax & RE_NO_BK_BRACES)
  1670.                       goto unfetch_interval;
  1671.                     else
  1672.                       return REG_EBRACE;
  1673.                   }
  1674.  
  1675.                 GET_UNSIGNED_NUMBER (lower_bound);
  1676.  
  1677.                 if (c == ',')
  1678.                   {
  1679.                     GET_UNSIGNED_NUMBER (upper_bound);
  1680.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  1681.                   }
  1682.  
  1683.                 if (upper_bound < 0)
  1684.                   upper_bound = lower_bound;
  1685.  
  1686.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  1687.                     || lower_bound > upper_bound)
  1688.                   {
  1689.                     if (syntax & RE_NO_BK_BRACES)
  1690.                       goto unfetch_interval;
  1691.                     else 
  1692.                       return REG_BADBR;
  1693.                   }
  1694.  
  1695.                 if (!(syntax & RE_NO_BK_BRACES)) 
  1696.                   {
  1697.                     if (c != '\\') return REG_EBRACE;
  1698.  
  1699.                     PATFETCH (c);
  1700.                   }
  1701.  
  1702.                 if (c != '}')
  1703.                   {
  1704.                     if (syntax & RE_NO_BK_BRACES)
  1705.                       goto unfetch_interval;
  1706.                     else 
  1707.                       return REG_BADBR;
  1708.                   }
  1709.  
  1710.                 /* We just parsed a valid interval.  */
  1711.  
  1712.                 /* If it's invalid to have no preceding re.  */
  1713.                 if (!laststart)
  1714.                   {
  1715.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  1716.                       return REG_BADRPT;
  1717.                     else if (syntax & RE_CONTEXT_INDEP_OPS)
  1718.                       laststart = b;
  1719.                     else
  1720.                       goto unfetch_interval;
  1721.                   }
  1722.  
  1723.                 /* If upper_bound is zero, don't want to succeed at all; 
  1724.                    jump from laststart to b + 3, which will be the end of
  1725.                    the buffer after this jump is inserted.  */
  1726.                  if (upper_bound == 0)
  1727.                    {
  1728.                      GET_BUFFER_SPACE (3);
  1729.                      insert_jump (no_pop_jump, laststart, b + 3, b);
  1730.                      b += 3;
  1731.                    }
  1732.  
  1733.                  /* Otherwise, after lower_bound number of succeeds, jump
  1734.                     to after the no_pop_jump_n which will be inserted at
  1735.                     the end of the buffer, and insert that
  1736.                     no_pop_jump_n.  */
  1737.                  else 
  1738.                    { /* Set to 5 if only one repetition is allowed and
  1739.                         hence no no_pop_jump_n is inserted at the current
  1740.                         end of the buffer.  Otherwise, need 10 bytes total
  1741.                         for the succeed_n and the no_pop_jump_n.  */
  1742.                      unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  1743.  
  1744.                      GET_BUFFER_SPACE (slots_needed);
  1745.                      /* Initialize the succeed_n to n, even though it will
  1746.                         be set by its attendant set_number_at, because
  1747.                         re_compile_fastmap will need to know it.  Jump to
  1748.                         what the end of buffer will be after inserting
  1749.                         this succeed_n and possibly appending a
  1750.                         no_pop_jump_n.  */
  1751.                      insert_jump_n (succeed_n, laststart, b + slots_needed, 
  1752.                                     b, lower_bound);
  1753.                      b += 5;     /* Just increment for the succeed_n here.  */
  1754.  
  1755.  
  1756.                     /* More than one repetition is allowed, so put in at
  1757.                        the end of the buffer a backward jump from b to the
  1758.                        succeed_n we put in above.  By the time we've gotten
  1759.                        to this jump when matching, we'll have matched once
  1760.                        already, so jump back only upper_bound - 1 times.  */
  1761.                      if (upper_bound > 1)
  1762.                        {
  1763.                          store_jump_n (b, no_pop_jump_n, laststart, 
  1764.                                        upper_bound - 1);
  1765.                          b += 5;
  1766.  
  1767.                          /* When hit this when matching, reset the
  1768.                             preceding no_pop_jump_n's n to upper_bound - 1.  */
  1769.                          PAT_PUSH (set_number_at);
  1770.  
  1771.                          /* Only need to get space for the numbers.  */
  1772.                          GET_BUFFER_SPACE (4);
  1773.                          STORE_NUMBER_AND_INCR (b, -5);
  1774.                          STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1775.                        }
  1776.  
  1777.                      /* When hit this when matching, set the succeed_n's n.  */
  1778.                      GET_BUFFER_SPACE (5);
  1779.                      insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1780.                      b += 5;
  1781.                    }
  1782.                 pending_exact = 0;
  1783.                 beg_interval = NULL;
  1784.  
  1785.                 if (following_left_brace)
  1786.                   goto normal_char;        
  1787.               }
  1788.               break;
  1789.  
  1790.             unfetch_interval:
  1791.               /* If an invalid interval, match the characters as literals.  */
  1792.                assert (beg_interval);
  1793.                p = beg_interval;
  1794.                beg_interval = NULL;
  1795.  
  1796.                /* normal_char and normal_backslash need `c'.  */
  1797.                PATFETCH (c);    
  1798.  
  1799.                if (!(syntax & RE_NO_BK_BRACES))
  1800.                  {
  1801.                    if (p > pattern  &&  p[-1] == '\\')
  1802.                      goto normal_backslash;
  1803.                  }
  1804.                goto normal_char;
  1805.  
  1806. #ifdef emacs
  1807.             /* There is no way to specify the before_dot and after_dot
  1808.                operators.  rms says this is ok.  --karl  */
  1809.             case '=':
  1810.               PAT_PUSH (at_dot);
  1811.               break;
  1812.  
  1813.             case 's':    
  1814.               laststart = b;
  1815.               PATFETCH (c);
  1816.               PAT_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  1817.               break;
  1818.  
  1819.             case 'S':
  1820.               laststart = b;
  1821.               PATFETCH (c);
  1822.               PAT_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  1823.               break;
  1824. #endif /* emacs */
  1825.  
  1826.  
  1827.             case 'w':
  1828.               laststart = b;
  1829.               PAT_PUSH (wordchar);
  1830.               break;
  1831.  
  1832.  
  1833.             case 'W':
  1834.               laststart = b;
  1835.               PAT_PUSH (notwordchar);
  1836.               break;
  1837.  
  1838.  
  1839.             case '<':
  1840.               PAT_PUSH (wordbeg);
  1841.               break;
  1842.  
  1843.             case '>':
  1844.               PAT_PUSH (wordend);
  1845.               break;
  1846.  
  1847.             case 'b':
  1848.               PAT_PUSH (wordbound);
  1849.               break;
  1850.  
  1851.             case 'B':
  1852.               PAT_PUSH (notwordbound);
  1853.               break;
  1854.  
  1855.             case '`':
  1856.               PAT_PUSH (begbuf);
  1857.               break;
  1858.  
  1859.             case '\'':
  1860.               PAT_PUSH (endbuf);
  1861.               break;
  1862.  
  1863.             case '1':
  1864.             case '2':
  1865.             case '3':
  1866.             case '4':
  1867.             case '5':
  1868.             case '6':
  1869.             case '7':
  1870.             case '8':
  1871.             case '9':
  1872.               if (syntax & RE_NO_BK_REFS)
  1873.                 goto normal_char;
  1874.  
  1875.               c1 = c - '0';
  1876.  
  1877.               if (c1 > regnum)
  1878.                 {
  1879.                   if (syntax & RE_NO_MISSING_BK_REF)
  1880.                     return REG_ESUBREG;
  1881.                   else
  1882.                     goto normal_char;
  1883.                 }
  1884.  
  1885.               /* Can't back reference to a subexpression if inside of it.  */
  1886.               if (group_in_compile_stack (compile_stack, c1))
  1887.                 goto normal_char;
  1888.  
  1889.               laststart = b;
  1890.               PAT_PUSH_2 (duplicate, c1);
  1891.               break;
  1892.  
  1893.  
  1894.             case '+':
  1895.             case '?':
  1896.               if (syntax & RE_BK_PLUS_QM)
  1897.                 goto handle_plus;
  1898.               else
  1899.                 goto normal_backslash;
  1900.  
  1901.             default:
  1902.             normal_backslash:
  1903.               /* You might think it would be useful for \ to mean
  1904.                  not to translate; but if we don't translate it
  1905.                  it will never match anything.  */
  1906.               c = TRANSLATE (c);
  1907.               goto normal_char;
  1908.             }
  1909.           break;
  1910.  
  1911.  
  1912.     default:
  1913.         /* Expects the character in `c'.  */
  1914.     normal_char:
  1915.           /* If no exactn currently being built.  */
  1916.           if (!pending_exact 
  1917.  
  1918.               /* If last exactn not at current position.  */
  1919.               || pending_exact + *pending_exact + 1 != b
  1920.               
  1921.               /* We have only one byte following the exactn for the count.  */
  1922.           || *pending_exact == (1 << BYTEWIDTH) - 1
  1923.  
  1924.               /* If followed by a repetition operator.  */
  1925.               || *p == '*' || *p == '^'
  1926.           || ((syntax & RE_BK_PLUS_QM)
  1927.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1928.           : (*p == '+' || *p == '?'))
  1929.           || ((syntax & RE_INTERVALS)
  1930.                   && ((syntax & RE_NO_BK_BRACES)
  1931.               ? *p == '{'
  1932.                       : (p[0] == '\\' && p[1] == '{'))))
  1933.         {
  1934.           /* Start building a new exactn.  */
  1935.               
  1936.               laststart = b;
  1937.  
  1938.           PAT_PUSH_2 (exactn, 0);
  1939.           pending_exact = b - 1;
  1940.             }
  1941.             
  1942.       PAT_PUSH (c);
  1943.           (*pending_exact)++;
  1944.       break;
  1945.         } /* switch (c) */
  1946.     } /* while p != pend */
  1947.  
  1948.   
  1949.   /* Through the pattern now.  */
  1950.   
  1951.   if (fixup_alt_jump)
  1952.     store_jump (fixup_alt_jump, jump_past_next_alt, b);
  1953.  
  1954.   if (!COMPILE_STACK_EMPTY) 
  1955.     return REG_EPAREN;
  1956.  
  1957.   free (compile_stack.stack);
  1958.  
  1959.   /* We have succeeded; set the length of the buffer.  */
  1960.   bufp->used = b - bufp->buffer;
  1961.   return REG_NOERROR;
  1962. } /* regex_compile */
  1963.  
  1964. /* Subroutines for regex_compile.  */
  1965.  
  1966. /* Store a jump of the form <OPCODE> <relative address>.
  1967.    Store in the location FROM a jump operation to jump to relative
  1968.    address FROM - TO.  OPCODE is the opcode to store.  */
  1969.  
  1970. static void
  1971. store_jump (from, op, to)
  1972.      unsigned char *from, *to;
  1973.      re_opcode_t op;
  1974. {
  1975.   from[0] = (unsigned char) op;
  1976.   STORE_NUMBER (from + 1, to - (from + 3));
  1977. }
  1978.  
  1979.  
  1980. /* Open up space before char FROM, and insert there a jump to TO.
  1981.    CURRENT_END gives the end of the storage not in use, so we know 
  1982.    how much data to copy up. OP is the opcode of the jump to insert.
  1983.  
  1984.    If you call this function, you must zero out pending_exact.  */
  1985.  
  1986. static void
  1987. insert_jump (op, from, to, current_end)
  1988.      re_opcode_t op;
  1989.      unsigned char *from, *to, *current_end;
  1990. {
  1991.   register unsigned char *pfrom = current_end;   /* Copy from here...  */
  1992.   register unsigned char *pto = current_end + 3; /* ...to here.  */
  1993.  
  1994.   while (pfrom != from)                   
  1995.     *--pto = *--pfrom;
  1996.     
  1997.   store_jump (from, op, to);
  1998. }
  1999.  
  2000.  
  2001. /* Store a jump of the form <opcode> <relative address> <n>.
  2002.  
  2003.    Store in the location FROM a jump operation to jump to relative
  2004.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  2005.    jump uses, say, to decide how many times to jump.
  2006.    
  2007.    If you call this function, you must zero out pending_exact.  */
  2008.  
  2009. static void
  2010. store_jump_n (from, op, to, n)
  2011.      unsigned char *from, *to;
  2012.      re_opcode_t op;
  2013.      unsigned n;
  2014. {
  2015.   from[0] = (unsigned char) op;
  2016.   STORE_NUMBER (from + 1, to - (from + 3));
  2017.   STORE_NUMBER (from + 3, n);
  2018. }
  2019.  
  2020.  
  2021. /* Similar to insert_jump, but handles a jump which needs an extra
  2022.    number to handle minimum and maximum cases.  Open up space at
  2023.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  2024.    end of the storage in use, so we know how much data to copy up. OP is
  2025.    the opcode of the jump to insert.
  2026.  
  2027.    If you call this function, you must zero out pending_exact.  */
  2028.  
  2029. static void
  2030. insert_jump_n (op, from, to, current_end, n)
  2031.      re_opcode_t op;
  2032.      unsigned char *from, *to, *current_end;
  2033.      unsigned n;
  2034. {
  2035.   register unsigned char *pfrom = current_end;
  2036.   register unsigned char *pto = current_end + 5;
  2037.  
  2038.   while (pfrom != from)
  2039.     *--pto = *--pfrom;
  2040.     
  2041.   store_jump_n (from, op, to, n);
  2042. }
  2043.  
  2044.  
  2045. /* Open up space at location THERE, and insert operation OP followed by
  2046.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  2047.    we know how much data to copy up.
  2048.  
  2049.    If you call this function, you must zero out pending_exact.  */
  2050.  
  2051. static void
  2052. insert_op_2 (op, there, current_end, num_1, num_2)
  2053.      re_opcode_t op;
  2054.      unsigned char *there, *current_end;
  2055.      int num_1, num_2;
  2056. {
  2057.   register unsigned char *pfrom = current_end;
  2058.   register unsigned char *pto = current_end + 5;
  2059.  
  2060.   while (pfrom != there)                   
  2061.     *--pto = *--pfrom;
  2062.   
  2063.   there[0] = (unsigned char) op;
  2064.   STORE_NUMBER (there + 1, num_1);
  2065.   STORE_NUMBER (there + 3, num_2);
  2066. }
  2067.  
  2068.  
  2069. /* Return true if the pattern position P is at a close-group or
  2070.    alternation operator, or if it is a newline and RE_NEWLINE_ORDINARY
  2071.    is not set in SYNTAX.  Before checking, though, we skip past all
  2072.    operators that match the empty string.  
  2073.    
  2074.    This is not quite the dual of what happens with ^.  There, we can
  2075.    easily check if the (sub)pattern so far can match only the empty
  2076.    string, because we have seen the pattern, and `laststart' is set to
  2077.    exactly that.  But we cannot easily look at the pattern yet to come
  2078.    to see if it matches the empty string; that would require us to compile
  2079.    the pattern, then go back and analyze the pattern after every
  2080.    endline.  POSIX required this at one point (that $ be in a
  2081.    ``trailing'' position to be considered an anchor), so we implemented
  2082.    it, but it was slow and took lots of code, and we were never really
  2083.    convinced it worked in all cases.  So now it's gone, and we live with
  2084.    the slight inconsistency between ^ and $.  */
  2085.  
  2086. static boolean
  2087. at_endline_op_p (p, pend, syntax)
  2088.     const char *p, *pend;
  2089.     int syntax;
  2090. {
  2091.   boolean context_indep = !!(syntax & RE_CONTEXT_INDEP_ANCHORS);
  2092.   
  2093.   /* Skip past operators that match the empty string.  (Except we don't
  2094.      handle empty groups.)  */
  2095.   while (p < pend)
  2096.     {
  2097.       if (context_indep && (*p == '^' || *p == '$'))
  2098.         p++;
  2099.       
  2100.       /* All others start with \.  */
  2101.       else if (*p == '\\' && p + 1 < pend 
  2102.                  && (p[1] == 'b' || p[1] == 'B'
  2103.                      || p[1] == '<' || p[1] == '>'
  2104.              || p[1] == '`' || p[1] == '\''
  2105. #ifdef emacs
  2106.              || p[1] == '='
  2107. #endif
  2108.             ))
  2109.          p += 2;
  2110.        
  2111.        else /* Not an empty string operator.  */
  2112.          break;
  2113.     }
  2114.     
  2115.   /* See what we're at now.  */
  2116.   return p < pend
  2117.     && ((!(syntax & RE_NEWLINE_ORDINARY) && *p == '\n')
  2118.         || (syntax & RE_NO_BK_PARENS
  2119.            ? *p == ')'
  2120.            : *p == '\\' && p + 1 < pend && p[1] == ')')
  2121.         || (syntax & RE_NO_BK_VBAR
  2122.             ? *p == '|'
  2123.             : (*p == '\\' && p + 1 < pend && p[1] == '|')));
  2124. }
  2125.  
  2126.  
  2127. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  2128.    false if it's not.  */
  2129.  
  2130. static boolean
  2131. group_in_compile_stack (compile_stack, regnum)
  2132.     compile_stack_type compile_stack;
  2133.     regnum_t regnum;
  2134. {
  2135.   int this_element;
  2136.  
  2137.   for (this_element = compile_stack.avail - 1;  
  2138.        this_element >= 0; 
  2139.        this_element--)
  2140.     if (compile_stack.stack[this_element].regnum == regnum)
  2141.       return true;
  2142.  
  2143.   return false;
  2144. }
  2145.  
  2146. /* Failure stack declarations and macros; both re_compile_fastmap and
  2147.    re_match_2 use a failure stack.  These have to be macros because of
  2148.    REGEX_ALLOCATE.  */
  2149.    
  2150.  
  2151. /* Number of failure points for which to initially allocate space
  2152.    when matching.  If this number is exceeded, we allocate more
  2153.    space---so it is not a hard limit.  */
  2154. #ifndef INIT_FAILURE_ALLOC
  2155. #define INIT_FAILURE_ALLOC 5
  2156. #endif
  2157.  
  2158. /* Roughly the maximum number of failure points on the stack.  Would be
  2159.    exactly that if always used MAX_FAILURE_SPACE each time we failed.
  2160.    This is a variable only so users of regex can assign to it; we never
  2161.    change it ourselves.  */
  2162. int re_max_failures = 2000;
  2163.  
  2164. typedef const unsigned char *failure_stack_elt_t;
  2165.  
  2166. typedef struct
  2167. {
  2168.   failure_stack_elt_t *stack;
  2169.   unsigned size;
  2170.   unsigned avail;            /* Offset of next open position.  */
  2171. } failure_stack_type;
  2172.  
  2173. #define FAILURE_STACK_EMPTY()     (failure_stack.avail == 0)
  2174. #define FAILURE_STACK_PTR_EMPTY() (failure_stack_ptr->avail == 0)
  2175. #define FAILURE_STACK_FULL()      (failure_stack.avail == failure_stack.size)
  2176. #define FAILURE_STACK_TOP()       (failure_stack.stack[failure_stack.avail])
  2177.  
  2178.  
  2179. /* Initialize FAILURE_STACK.  Return 1 if success, 0 if not.  */
  2180.  
  2181. #define INIT_FAILURE_STACK(failure_stack)                \
  2182.   ((failure_stack).stack = (failure_stack_elt_t *)            \
  2183.     REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (failure_stack_elt_t)), \
  2184.   (failure_stack).stack == NULL                        \
  2185.   ? 0                                    \
  2186.   : ((failure_stack).size = INIT_FAILURE_ALLOC,                \
  2187.      (failure_stack).avail = 0,                        \
  2188.      1))
  2189.  
  2190.  
  2191. /* Double the size of FAILURE_STACK, up to approximately
  2192.    `re_max_failures' items.
  2193.  
  2194.    Return 1 if succeeds, and 0 if either ran out of memory
  2195.    allocating space for it or it was already too large.  
  2196.    
  2197.    REGEX_REALLOCATE requires `destination' be declared.   */
  2198.  
  2199. #define DOUBLE_FAILURE_STACK(failure_stack)                \
  2200.   ((failure_stack).size > re_max_failures * MAX_FAILURE_ITEMS        \
  2201.    ? 0                                    \
  2202.    : ((failure_stack).stack = (failure_stack_elt_t *)            \
  2203.         REGEX_REALLOCATE ((failure_stack).stack,             \
  2204.           ((failure_stack).size << 1) * sizeof (failure_stack_elt_t)),    \
  2205.                                     \
  2206.       (failure_stack).stack == NULL                    \
  2207.       ? 0                                \
  2208.       : ((failure_stack).size <<= 1,                     \
  2209.          1)))
  2210.  
  2211.  
  2212. /* Push PATTERN_OP on FAILURE_STACK. 
  2213.  
  2214.    Return 1 if was able to do so and 0 if ran out of memory allocating
  2215.    space to do so.  */
  2216. #define PUSH_PATTERN_OP(pattern_op, failure_stack)            \
  2217.   ((FAILURE_STACK_FULL ()                        \
  2218.     && !DOUBLE_FAILURE_STACK (failure_stack))                \
  2219.     ? 0                                    \
  2220.     : ((failure_stack).stack[(failure_stack).avail++] = pattern_op,    \
  2221.        1))
  2222.  
  2223. /* This pushes an item onto the failure stack.  Must be a four-byte
  2224.    value.  Assumes the variable `failure_stack'.  Probably should only
  2225.    be called from within `PUSH_FAILURE_POINT'.  */
  2226. #define PUSH_FAILURE_ITEM(item)                        \
  2227.   failure_stack.stack[failure_stack.avail++] = (failure_stack_elt_t) item
  2228.  
  2229. /* The complement operation.  Assumes stack is nonempty, and pointed to
  2230.    `failure_stack_ptr'.  */
  2231. #define POP_FAILURE_ITEM()                        \
  2232.   failure_stack_ptr->stack[--failure_stack_ptr->avail]
  2233.  
  2234. /* Used to omit pushing failure point id's when we're not debugging.  */
  2235. #ifdef DEBUG
  2236. #define DEBUG_PUSH PUSH_FAILURE_ITEM
  2237. #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_ITEM ()
  2238. #else
  2239. #define DEBUG_PUSH(item)
  2240. #define DEBUG_POP(item_addr)
  2241. #endif
  2242.  
  2243.  
  2244. /* Push the information about the state we will need
  2245.    if we ever fail back to it.  
  2246.    
  2247.    Requires variables failure_stack, regstart, regend, reg_info, and
  2248.    num_regs be declared.  DOUBLE_FAILURE_STACK requires `destination' be
  2249.    declared.
  2250.    
  2251.    Does `return FAILURE_CODE' if runs out of memory.  */
  2252.  
  2253. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code)    \
  2254.   do {                                    \
  2255.     char *destination;                            \
  2256.     /* Must be int, so when we don't save any registers, the arithmetic    \
  2257.        of 0 + -1 isn't done as unsigned.  */                \
  2258.     int this_reg;                            \
  2259.                                         \
  2260.     DEBUG_STATEMENT (failure_id++);                    \
  2261.     DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id);        \
  2262.     DEBUG_PRINT2 ("  Before push, next avail: %d\n", (failure_stack).avail);\
  2263.     DEBUG_PRINT2 ("                     size: %d\n", (failure_stack).size);\
  2264.                                     \
  2265.     DEBUG_PRINT2 ("  slots needed: %d\n", NUM_FAILURE_ITEMS);        \
  2266.     DEBUG_PRINT2 ("     available: %d\n", REMAINING_AVAIL_SLOTS);    \
  2267.                                     \
  2268.     /* Ensure we have enough space allocated for what we will push.  */    \
  2269.     while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS)            \
  2270.       {                                    \
  2271.         if (!DOUBLE_FAILURE_STACK (failure_stack))            \
  2272.           return failure_code;                        \
  2273.                                     \
  2274.         DEBUG_PRINT2 ("\n  Doubled stack; size now: %d\n",        \
  2275.                (failure_stack).size);                \
  2276.         DEBUG_PRINT2 ("  slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  2277.       }                                    \
  2278.                                     \
  2279.     /* Push the info, starting with the registers.  */            \
  2280.     DEBUG_PRINT1 ("\n");                        \
  2281.                                     \
  2282.     for (this_reg = lowest_active_reg; this_reg <= highest_active_reg;    \
  2283.          this_reg++)                            \
  2284.       {                                    \
  2285.     DEBUG_PRINT2 ("  Pushing reg: %d\n", this_reg);            \
  2286.         DEBUG_STATEMENT (num_regs_pushed++);                \
  2287.                                     \
  2288.     DEBUG_PRINT2 ("    start: 0x%x\n", regstart[this_reg]);        \
  2289.         PUSH_FAILURE_ITEM (regstart[this_reg]);                \
  2290.                                                                         \
  2291.     DEBUG_PRINT2 ("    end: 0x%x\n", regend[this_reg]);        \
  2292.         PUSH_FAILURE_ITEM (regend[this_reg]);                \
  2293.                                     \
  2294.     DEBUG_PRINT2 ("    info: 0x%x\n      ", reg_info[this_reg]);    \
  2295.         DEBUG_PRINT2 (" match_nothing=%d",                \
  2296.                       REG_MATCH_NULL_STRING_P (reg_info[this_reg]));    \
  2297.         DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg]));    \
  2298.         DEBUG_PRINT2 (" matched_something=%d",                \
  2299.                       MATCHED_SOMETHING (reg_info[this_reg]));        \
  2300.         DEBUG_PRINT2 (" ever_matched=%d",                \
  2301.                       EVER_MATCHED_SOMETHING (reg_info[this_reg]));    \
  2302.     DEBUG_PRINT1 ("\n");                        \
  2303.         PUSH_FAILURE_ITEM (reg_info[this_reg].word);            \
  2304.       }                                    \
  2305.                                     \
  2306.     DEBUG_PRINT2 ("  Pushing low active reg: %d\n", lowest_active_reg);    \
  2307.     PUSH_FAILURE_ITEM (lowest_active_reg);                \
  2308.                                     \
  2309.     DEBUG_PRINT2 ("  Pushing high active reg: %d\n", highest_active_reg);\
  2310.     PUSH_FAILURE_ITEM (highest_active_reg);                \
  2311.                                     \
  2312.     DEBUG_PRINT2 ("  Pushing pattern 0x%x: ", pattern_place);        \
  2313.     DEBUG_COMPILED_PATTERN_PRINTER (bufp, pattern_place, pend);        \
  2314.     PUSH_FAILURE_ITEM (pattern_place);                    \
  2315.                                     \
  2316.     DEBUG_PRINT2 ("  Pushing string 0x%x: `", string_place);        \
  2317.     DEBUG_DOUBLE_STRING_PRINTER (string_place, string1, size1, string2, \
  2318.                  size2);                \
  2319.     DEBUG_PRINT1 ("'\n");                        \
  2320.     PUSH_FAILURE_ITEM (string_place);                    \
  2321.                                     \
  2322.     DEBUG_PRINT2 ("  Pushing failure id: %u\n", failure_id);        \
  2323.     DEBUG_PUSH (failure_id);                        \
  2324.   } while (0)
  2325.  
  2326. /* This is the number of items that are pushed and popped on the stack
  2327.    for each register.  */
  2328. #define NUM_REG_ITEMS  3
  2329.  
  2330. /* Individual items aside from the registers.  */
  2331. #ifdef DEBUG
  2332. #define NUM_NONREG_ITEMS 5 /* Includes failure point id.  */
  2333. #else
  2334. #define NUM_NONREG_ITEMS 4
  2335. #endif
  2336.  
  2337. /* We push at most this many items on the stack.  */
  2338. #define MAX_FAILURE_ITEMS                        \
  2339.   ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  2340.  
  2341. /* We actually push this many items.  */
  2342. #define NUM_FAILURE_ITEMS                        \
  2343.   ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS     \
  2344.     + NUM_NONREG_ITEMS)
  2345.  
  2346. /* How many items can still be added to the stack without overflowing it.  */
  2347. #define REMAINING_AVAIL_SLOTS                        \
  2348.   ((failure_stack).size - (failure_stack).avail)
  2349.  
  2350. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2351.    BUFP.  A fastmap records which of the (1 << BYTEWIDTH) possible
  2352.    characters can start a string that matches the pattern.  This fastmap
  2353.    is used by re_search to skip quickly over impossible starting points.
  2354.  
  2355.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2356.    area as BUFP->fastmap.  The other components of BUFP describe the
  2357.    pattern to be used.
  2358.    
  2359.    We set the `can_be_null' and `fastmap_accurate' fields in the pattern
  2360.  
  2361.    Returns 0 if it can compile a fastmap.  Returns -2 if there is an
  2362.    internal error.   */
  2363.  
  2364. int
  2365. re_compile_fastmap (bufp)
  2366.      struct re_pattern_buffer *bufp;
  2367. {
  2368.   int j, k;
  2369.   failure_stack_type failure_stack;
  2370. #ifndef REGEX_MALLOC
  2371.   char *destination;
  2372. #endif
  2373.   /* We don't push any register information onto the failure stack.  */
  2374.   unsigned num_regs = 0;
  2375.   
  2376.   register char *fastmap = bufp->fastmap;
  2377.   unsigned char *pattern = bufp->buffer;
  2378.   unsigned long size = bufp->used;
  2379.   const unsigned char *p = pattern;
  2380.   register unsigned char *pend = pattern + size;
  2381.  
  2382.   INIT_FAILURE_STACK (failure_stack);
  2383.  
  2384.   bzero (fastmap, 1 << BYTEWIDTH);
  2385.   bufp->fastmap_accurate = 1; /* It will be when we're done.  */
  2386.   bufp->can_be_null = 0;
  2387.       
  2388.   while (p)
  2389.     {
  2390.       boolean is_a_succeed_n = false;
  2391.       
  2392.       if (p == pend)
  2393.         if (FAILURE_STACK_EMPTY ())      
  2394.           {
  2395.             bufp->can_be_null = 1;
  2396.             break;
  2397.           }
  2398.         else
  2399.           p = failure_stack.stack[--failure_stack.avail];
  2400.           
  2401. #ifdef SWITCH_ENUM_BUG
  2402.       switch ((int) ((re_opcode_t) *p++))
  2403. #else
  2404.       switch ((re_opcode_t) *p++)
  2405. #endif
  2406.     {
  2407.     case exactn:
  2408.           fastmap[p[1]] = 1;
  2409.       break;
  2410.  
  2411.  
  2412.         case charset:
  2413.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2414.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2415.               fastmap[j] = 1;
  2416.       break;
  2417.  
  2418.  
  2419.     case charset_not:
  2420.       /* Chars beyond end of map must be allowed.  */
  2421.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  2422.             fastmap[j] = 1;
  2423.  
  2424.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2425.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2426.               fastmap[j] = 1;
  2427.           break;
  2428.  
  2429.  
  2430.         case no_op:
  2431.         case begline:
  2432.     case begbuf:
  2433.     case endbuf:
  2434.     case wordbound:
  2435.     case notwordbound:
  2436.     case wordbeg:
  2437.     case wordend:
  2438.           continue;
  2439.  
  2440.  
  2441.     case endline:
  2442.       if (!bufp->can_be_null)
  2443.         bufp->can_be_null = 2;
  2444.       break;
  2445.  
  2446.  
  2447.     case no_pop_jump_n:
  2448.         case pop_failure_jump:
  2449.     case maybe_pop_jump:
  2450.     case no_pop_jump:
  2451.         case jump_past_next_alt:
  2452.     case dummy_failure_jump:
  2453.           EXTRACT_NUMBER_AND_INCR (j, p);
  2454.       p += j;    
  2455.       if (j > 0)
  2456.         continue;
  2457.             
  2458.           /* Jump backward reached implies we just went through
  2459.              the body of a loop and matched nothing.  Opcode jumped to
  2460.              should be an on_failure_jump or succeed_n.  Just treat it
  2461.              like an ordinary jump.  For a * loop, it has pushed its
  2462.              failure point already; if so, discard that as redundant.  */
  2463.  
  2464.           if ((re_opcode_t) *p != on_failure_jump
  2465.           && (re_opcode_t) *p != succeed_n)
  2466.         continue;
  2467.  
  2468.           p++;
  2469.           EXTRACT_NUMBER_AND_INCR (j, p);
  2470.           p += j;        
  2471.       
  2472.           /* If what's on the stack is where we are now, pop it.  */
  2473.           if (!FAILURE_STACK_EMPTY () 
  2474.           && failure_stack.stack[failure_stack.avail - 1] == p)
  2475.             failure_stack.avail--;
  2476.  
  2477.           continue;
  2478.  
  2479.  
  2480.         case on_failure_jump:
  2481.     handle_on_failure_jump:
  2482.           EXTRACT_NUMBER_AND_INCR (j, p);
  2483.  
  2484.           /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  2485.              end of the pattern.  We don't want to push such a point,
  2486.              since when we restore it above, entering the switch will
  2487.              increment `p' past the end of the pattern.  We don't need
  2488.              to push such a point since there can't be any more
  2489.              possibilities for the fastmap beyond pend.  */
  2490.           if (p + j < pend)
  2491.             {
  2492.               if (!PUSH_PATTERN_OP (p + j, failure_stack))
  2493.                 return -2;
  2494.             }
  2495.  
  2496.           if (is_a_succeed_n)
  2497.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  2498.  
  2499.           continue;
  2500.  
  2501.  
  2502.     case succeed_n:
  2503.       is_a_succeed_n = true;
  2504.  
  2505.           /* Get to the number of times to succeed.  */
  2506.           p += 2;        
  2507.  
  2508.           /* Increment p past the n for when k != 0.  */
  2509.           EXTRACT_NUMBER_AND_INCR (k, p);
  2510.           if (k == 0)
  2511.         {
  2512.               p -= 4;
  2513.               goto handle_on_failure_jump;
  2514.             }
  2515.           continue;
  2516.  
  2517.  
  2518.     case set_number_at:
  2519.           p += 4;
  2520.           continue;
  2521.  
  2522.  
  2523.     case start_memory:
  2524.         case stop_memory:
  2525.       p += 2;
  2526.       continue;
  2527.  
  2528.  
  2529.         /* I don't understand this case (any of it).  --karl  */
  2530.     case duplicate:
  2531.       bufp->can_be_null = 1;
  2532.       fastmap['\n'] = 1;
  2533.  
  2534.  
  2535.         case anychar:
  2536.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2537.         if (j != '\n')
  2538.           fastmap[j] = 1;
  2539.       if (bufp->can_be_null)
  2540.         return 0;
  2541.  
  2542.           /* Don't return; check the alternative paths
  2543.          so we can set can_be_null if appropriate.  */
  2544.       break;
  2545.  
  2546.  
  2547.     case wordchar:
  2548.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2549.         if (SYNTAX (j) == Sword)
  2550.           fastmap[j] = 1;
  2551.       break;
  2552.  
  2553.  
  2554.     case notwordchar:
  2555.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2556.         if (SYNTAX (j) != Sword)
  2557.           fastmap[j] = 1;
  2558.       break;
  2559.  
  2560.  
  2561. #ifdef emacs
  2562.         case before_dot:
  2563.     case at_dot:
  2564.     case after_dot:
  2565.           continue;
  2566.  
  2567.  
  2568.         case syntaxspec:
  2569.       k = *p++;
  2570.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2571.         if (SYNTAX (j) == (enum syntaxcode) k)
  2572.           fastmap[j] = 1;
  2573.       break;
  2574.  
  2575.  
  2576.     case notsyntaxspec:
  2577.       k = *p++;
  2578.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2579.         if (SYNTAX (j) != (enum syntaxcode) k)
  2580.           fastmap[j] = 1;
  2581.       break;
  2582. #endif /* not emacs */
  2583.  
  2584.         default:
  2585.           abort ();
  2586.         } /* switch *p++ */
  2587.  
  2588.       /* Getting here means we have successfully found the possible starting
  2589.          characters of one path of the pattern.  We need not follow this
  2590.          path any farther.  Instead, look at the next alternative
  2591.          remembered in the stack, or quit.  The test at the top of the
  2592.          loop does these things.  */
  2593.       p = pend;
  2594.     } /* while p */
  2595.  
  2596.   return 0;
  2597. } /* re_compile_fastmap  */
  2598.  
  2599. /* Searching routines.  */
  2600.  
  2601. /* Like re_search_2, below, but only one string is specified, and
  2602.    doesn't let you say where to stop matching. */
  2603.  
  2604. int
  2605. re_search (bufp, string, size, startpos, range, regs)
  2606.      struct re_pattern_buffer *bufp;
  2607.      const char *string;
  2608.      int size, startpos, range;
  2609.      struct re_registers *regs;
  2610. {
  2611.   return re_search_2 (bufp, NULL, 0, string, size, startpos, range, 
  2612.               regs, size);
  2613. }
  2614.  
  2615.  
  2616. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  2617.    virtual concatenation of STRING1 and STRING2, starting first at index
  2618.    STARTPOS, then at STARTPOS + 1, and so on.
  2619.    
  2620.    STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  2621.    
  2622.    RANGE is how far to scan while trying to match.  RANGE = 0 means try
  2623.    only at STARTPOS; in general, the last start tried is STARTPOS +
  2624.    RANGE.
  2625.    
  2626.    In REGS, return the indices of the virtual concatenation of STRING1
  2627.    and STRING2 that matched the entire BUFP->buffer and its contained
  2628.    subexpressions.
  2629.    
  2630.    Do not consider matching one past the index STOP in the virtual
  2631.    concatenation of STRING1 and STRING2.
  2632.  
  2633.    We return either the position in the strings at which the match was
  2634.    found, -1 if no match, or -2 if error (such as failure
  2635.    stack overflow).  */
  2636.  
  2637. int
  2638. re_search_2 (bufp, string1, size1, string2, size2, startpos, range,
  2639.          regs, stop)
  2640.      struct re_pattern_buffer *bufp;
  2641.      const char *string1, *string2;
  2642.      int size1, size2;
  2643.      int startpos;
  2644.      int range;
  2645.      struct re_registers *regs;
  2646.      int stop;
  2647. {
  2648.   int val;
  2649.   register char *fastmap = bufp->fastmap;
  2650.   register char *translate = bufp->translate;
  2651.   int total_size = size1 + size2;
  2652.   int endpos = startpos + range;
  2653.  
  2654.   /* Check for out-of-range STARTPOS.  */
  2655.   if (startpos < 0 || startpos > total_size)
  2656.     return -1;
  2657.     
  2658.   /* Fix up RANGE if it might eventually take us outside
  2659.      the virtual concatenation of STRING1 and STRING2.  */
  2660.   if (endpos < -1)
  2661.     range = -1 - startpos;
  2662.   else if (endpos > total_size)
  2663.     range = total_size - startpos;
  2664.  
  2665.   /* Update the fastmap now if not correct already.  */
  2666.   if (fastmap && !bufp->fastmap_accurate)
  2667.     if (re_compile_fastmap (bufp) == -2)
  2668.       return -2;
  2669.   
  2670.   /* If the search isn't to be a backwards one, don't waste time in a
  2671.      long search for a pattern that says it is anchored.  */
  2672.   if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf
  2673.       && range > 0)
  2674.     {
  2675.       if (startpos > 0)
  2676.     return -1;
  2677.       else
  2678.     range = 1;
  2679.     }
  2680.  
  2681.   for (;;)
  2682.     { 
  2683.       /* If a fastmap is supplied, skip quickly over characters that
  2684.          cannot be the start of a match.  If the pattern can match the
  2685.          null string, however, we don't want to skip over characters; we
  2686.          want the first null string.  */
  2687.       if (fastmap && startpos < total_size && !bufp->can_be_null)
  2688.     {
  2689.       if (range > 0)    /* Searching forwards.  */
  2690.         {
  2691.           register const char *d;
  2692.           register int lim = 0;
  2693.           int irange = range;
  2694.  
  2695.               if (startpos < size1 && startpos + range >= size1)
  2696.                 lim = range - (size1 - startpos);
  2697.  
  2698.           d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
  2699.    
  2700.               /* Written out as an if-else to avoid testing `translate'
  2701.                  inside the loop.  */
  2702.           if (translate)
  2703.         {
  2704.           while (range > lim
  2705.                          && !fastmap[(unsigned char) translate[*d++]])
  2706.             range--;
  2707.         }
  2708.           else
  2709.         {
  2710.           while (range > lim && !fastmap[(unsigned char) *d++])
  2711.             range--;
  2712.         }
  2713.  
  2714.           startpos += irange - range;
  2715.         }
  2716.       else                /* Searching backwards.  */
  2717.         {
  2718.           register char c
  2719.                 = (size1 == 0 || startpos >= size1
  2720.                    ? string2[startpos - size1] 
  2721.                    : string1[startpos]);
  2722.  
  2723.           if (translate
  2724.                   ? !fastmap[(unsigned char) translate[(unsigned char) c]]
  2725.                   : !fastmap[(unsigned char) c])
  2726.         goto advance;
  2727.         }
  2728.     }
  2729.  
  2730.       /* If can't match the null string, and that's all we have left, fail.  */
  2731.       if (range >= 0 && startpos == total_size
  2732.       && fastmap && bufp->can_be_null == 0)
  2733.     return -1;
  2734.  
  2735.       val = re_match_2 (bufp, string1, size1, string2, size2,
  2736.                     startpos, regs, stop);
  2737.       if (val >= 0)
  2738.     return startpos;
  2739.         
  2740.       if (val == -2)
  2741.     return -2;
  2742.  
  2743.     advance:
  2744.       if (!range) 
  2745.         break;
  2746.       else if (range > 0) 
  2747.         {
  2748.           range--; 
  2749.           startpos++;
  2750.         }
  2751.       else
  2752.         {
  2753.           range++; 
  2754.           startpos--;
  2755.         }
  2756.     }
  2757.   return -1;
  2758. } /* re_search_2 */
  2759.  
  2760. /* Declarations and macros for re_match_2.  */
  2761.  
  2762. static int bcmp_translate ();
  2763. static boolean alt_match_null_string_p (),
  2764.                common_op_match_null_string_p (),
  2765.                group_match_null_string_p ();
  2766. static void pop_failure_point ();
  2767.  
  2768.  
  2769. /* Structure for per-register (a.k.a. per-group) information.
  2770.    This must not be longer than one word, because we push this value
  2771.    onto the failure stack.  Other register information, such as the
  2772.    starting and ending positions (which are addresses), and the list of
  2773.    inner groups (which is a bits list) are maintained in separate
  2774.    variables.  
  2775.    
  2776.    We are making a (strictly speaking) nonportable assumption here: that
  2777.    the compiler will pack our bit fields into something that fits into
  2778.    the type of `word', i.e., is something that fits into one item on the
  2779.    failure stack.  */
  2780. typedef union
  2781. {
  2782.   failure_stack_elt_t word;
  2783.   struct
  2784.   {
  2785.       /* This field is one if this group can match the empty string,
  2786.          zero if not.  If not yet determined,  `MATCH_NOTHING_UNSET_VALUE'.  */
  2787. #define MATCH_NOTHING_UNSET_VALUE 3
  2788.     unsigned match_null_string_p : 2;
  2789.     unsigned is_active : 1;
  2790.     unsigned matched_something : 1;
  2791.     unsigned ever_matched_something : 1;
  2792.   } bits;
  2793. } register_info_type;
  2794.  
  2795. #define REG_MATCH_NULL_STRING_P(R)  ((R).bits.match_null_string_p)
  2796. #define IS_ACTIVE(R)  ((R).bits.is_active)
  2797. #define MATCHED_SOMETHING(R)  ((R).bits.matched_something)
  2798. #define EVER_MATCHED_SOMETHING(R)  ((R).bits.ever_matched_something)
  2799.  
  2800.  
  2801. /* Call this when have matched something; it sets `matched' flags for the
  2802.    registers corresponding to the group of which we currently are inside.  
  2803.    Also records whether this group ever matched something.  We only care
  2804.    about this information at `stop_memory', and then only about the
  2805.    previous time through the loop (if the group is starred or whatever).
  2806.    So it is ok to clear all the nonactive registers here.  */
  2807. #define SET_REGS_MATCHED()                        \
  2808.   do                                    \
  2809.     {                                    \
  2810.       unsigned r;                            \
  2811.       for (r = lowest_active_reg; r <= highest_active_reg; r++)        \
  2812.         {                                \
  2813.           MATCHED_SOMETHING (reg_info[r])                \
  2814.             = EVER_MATCHED_SOMETHING (reg_info[r])            \
  2815.             = 1;                            \
  2816.         }                                \
  2817.     }                                    \
  2818.   while (0)
  2819.  
  2820.  
  2821. /* This converts a pointer into one or the other of the strings into an
  2822.    offset from the beginning of that string.  */
  2823. #define POINTER_TO_OFFSET(pointer) IS_IN_FIRST_STRING (pointer)        \
  2824.                                 ? (pointer) - string1            \
  2825.                                 : (pointer) - string2 + size1
  2826.  
  2827. /* Registers are set to a sentinel value when they haven't yet matched
  2828.    anything.  */
  2829. #define REG_UNSET_VALUE ((char *) -1)
  2830. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  2831.  
  2832.  
  2833. /* Macros for dealing with the split strings in re_match_2.  */
  2834.  
  2835. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  2836.  
  2837. /* Call before fetching a character with *d.  This switches over to
  2838.    string2 if necessary.  */
  2839. #define PREFETCH                            \
  2840.   while (d == dend)                                \
  2841.     {                                    \
  2842.       /* End of string2 => fail.  */                    \
  2843.       if (dend == end_match_2)                         \
  2844.         goto fail;                            \
  2845.       /* End of string1 => advance to string2.  */             \
  2846.       d = string2;                                \
  2847.       dend = end_match_2;                        \
  2848.     }
  2849.  
  2850.  
  2851. /* Test if at very beginning or at very end of the virtual concatenation
  2852.    of string1 and string2.  If there is only one string, we've put it in
  2853.    string2.  */
  2854. #define AT_STRINGS_BEG  (d == (size1 ? string1 : string2) || !size2)
  2855. #define AT_STRINGS_END  (d == end2)    
  2856.  
  2857.  
  2858. /* Test if D points to a character which is word-constituent.  We have
  2859.    two special cases to check for: if past the end of string1, look at
  2860.    the first character in string2; and if before the beginning of
  2861.    string2, look at the last character in string1.
  2862.    
  2863.    We assume there is a string1, so use this in conjunction with
  2864.    AT_STRINGS_BEG.  */
  2865. #define LETTER_P(d)                            \
  2866.   (SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
  2867.    == Sword)
  2868.  
  2869. /* Test if the character before D and the one at D differ with respect
  2870.    to being word-constituent.  */
  2871. #define AT_WORD_BOUNDARY(d)                        \
  2872.   (AT_STRINGS_BEG || AT_STRINGS_END || LETTER_P (d - 1) != LETTER_P (d))
  2873.  
  2874.  
  2875. /* Free everything we malloc.  */
  2876. #ifdef REGEX_MALLOC
  2877. #define FREE_VARIABLES()                        \
  2878.   do {                                    \
  2879.     free (failure_stack.stack);                        \
  2880.     free (regstart);                            \
  2881.     free (regend);                            \
  2882.     free (old_regstart);                        \
  2883.     free (old_regend);                            \
  2884.     free (reg_info);                            \
  2885.     free (best_regstart);                        \
  2886.     free (best_regend);                            \
  2887.     reg_info = NULL;                            \
  2888.     failure_stack.stack = NULL;                        \
  2889.     regstart = regend = old_regstart = old_regend            \
  2890.       = best_regstart = best_regend = NULL;                \
  2891.   } while (0)
  2892. #else /* not REGEX_MALLOC */
  2893. #define FREE_VARIABLES() /* As nothing, since we use alloca. */
  2894. #endif /* not REGEX_MALLOC */
  2895.  
  2896.  
  2897. /* These values must meet several constraints.  They must not be valid
  2898.    register values; since we have a limit of 255 registers (because
  2899.    we use only one byte in the pattern for the register number), we can
  2900.    use numbers larger than 255.  They must differ by 1, because of
  2901.    NUM_FAILURE_ITEMS above.  And the value for the lowest register must
  2902.    be larger than the value for the highest register, so we do not try
  2903.    to actually save any registers when none are active.  */
  2904. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  2905. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  2906.  
  2907. /* Matching routines.  */
  2908.  
  2909. #ifndef emacs   /* Emacs never uses this.  */
  2910.  
  2911. /* re_match is like re_match_2 except it takes only a single string.  */
  2912.  
  2913. int
  2914. re_match (bufp, string, size, pos, regs)
  2915.      const struct re_pattern_buffer *bufp;
  2916.      const char *string;
  2917.      int size, pos;
  2918.      struct re_registers *regs;
  2919.  {
  2920.   return re_match_2 (bufp, NULL, 0, string, size, pos, regs, size); 
  2921. }
  2922. #endif /* not emacs */
  2923.  
  2924.  
  2925. /* re_match_2 matches the compiled pattern in BUFP against the
  2926.    the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  2927.    and SIZE2, respectively).  We start matching at POS, and stop
  2928.    matching at STOP.
  2929.    
  2930.    If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  2931.    store offsets for the substring each group matched in REGS.  (If
  2932.    BUFP->caller_allocated_regs is nonzero, we fill REGS->num_regs
  2933.    registers; if zero, we set REGS->num_regs to max (RE_NREGS,
  2934.    re_nsub+1) and allocate the space with malloc before filling.)
  2935.  
  2936.    We return -1 if no match, -2 if an internal error (such as the
  2937.    failure stack overflowing).  Otherwise, we return the length of the
  2938.    matched substring.  */
  2939.  
  2940. int
  2941. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  2942.      const struct re_pattern_buffer *bufp;
  2943.      const char *string1, *string2;
  2944.      int size1, size2;
  2945.      int pos;
  2946.      struct re_registers *regs;
  2947.      int stop;
  2948. {
  2949.   /* General temporaries.  */
  2950.   int mcnt;
  2951.   unsigned char *p1;
  2952.  
  2953.   /* Just past the end of the corresponding string.  */
  2954.   const char *end1, *end2;
  2955.  
  2956.   /* Pointers into string1 and string2, just past the last characters in
  2957.      each to consider matching.  */
  2958.   const char *end_match_1, *end_match_2;
  2959.  
  2960.   /* Where we are in the data, and the end of the current string.  */
  2961.   const char *d, *dend;
  2962.   
  2963.   /* Where we are in the pattern, and the end of the pattern.  */
  2964.   unsigned char *p = bufp->buffer;
  2965.   register unsigned char *pend = p + bufp->used;
  2966.  
  2967.   /* We use this to map every character in the string.  */
  2968.   char *translate = bufp->translate;
  2969.  
  2970.  /* Failure point stack.  Each place that can handle a failure further
  2971.     down the line pushes a failure point on this stack.  It consists of
  2972.     restart, regend, and reg_info for all registers corresponding to the
  2973.     subexpressions we're currently inside, plus the number of such
  2974.     registers, and, finally, two char *'s.  The first char * is where to
  2975.     resume scanning the pattern; the second one is where to resume
  2976.     scanning the strings.  If the latter is zero, the failure point is a
  2977.     ``dummy''; if a failure happens and the failure point is a dummy, it
  2978.     gets discarded and the next next one is tried.  */
  2979.   failure_stack_type failure_stack;
  2980. #ifdef DEBUG
  2981.   static unsigned failure_id = 0;
  2982. #endif
  2983.  
  2984.   /* We fill all the registers internally, independent of what we
  2985.      return, for use in backreferences.  The number here includes
  2986.      register zero.  */
  2987.   unsigned num_regs = bufp->re_nsub + 1;
  2988.   
  2989.   /* The currently active registers.  */
  2990.   unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  2991.   unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  2992.  
  2993.   /* Information on the contents of registers. These are pointers into
  2994.      the input strings; they record just what was matched (on this
  2995.      attempt) by a subexpression part of the pattern, that is, the
  2996.      regnum-th regstart pointer points to where in the pattern we began
  2997.      matching and the regnum-th regend points to right after where we
  2998.      stopped matching the regnum-th subexpression.  (The zeroth register
  2999.      keeps track of what the whole pattern matches.)  */
  3000.   const char **regstart
  3001.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3002.   const char **regend
  3003.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3004.  
  3005.   /* If a group that's operated upon by a repetition operator fails to
  3006.      match anything, then the register for its start will need to be
  3007.      restored because it will have been set to wherever in the string we
  3008.      are when we last see its open-group operator.  Similarly for a
  3009.      register's end.  */
  3010.   const char **old_regstart
  3011.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3012.   const char **old_regend
  3013.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3014.  
  3015.   /* The is_active field of reg_info helps us keep track of which (possibly
  3016.      nested) subexpressions we are currently in. The matched_something
  3017.      field of reg_info[reg_num] helps us tell whether or not we have
  3018.      matched any of the pattern so far this time through the reg_num-th
  3019.      subexpression.  These two fields get reset each time through any
  3020.      loop their register is in.  */
  3021.   register_info_type *reg_info = (register_info_type *) 
  3022.     REGEX_ALLOCATE (num_regs * sizeof (register_info_type));
  3023.  
  3024.   /* The following record the register info as found in the above
  3025.      variables when we find a match better than any we've seen before. 
  3026.      This happens as we backtrack through the failure points, which in
  3027.      turn happens only if we have not yet matched the entire string.  */
  3028.   unsigned best_regs_set = 0;
  3029.   const char **best_regstart
  3030.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3031.   const char **best_regend
  3032.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3033.  
  3034.   /* Used when we pop values we don't care about.  */
  3035.   const char **reg_dummy
  3036.     = (const char **) REGEX_ALLOCATE (num_regs * sizeof (char *));
  3037.   register_info_type *reg_info_dummy = (register_info_type *) 
  3038.     REGEX_ALLOCATE (num_regs * sizeof (register_info_type));
  3039.  
  3040. #ifdef DEBUG
  3041.   /* Counts the total number of registers pushed.  */
  3042.   unsigned num_regs_pushed = 0;     
  3043. #endif
  3044.  
  3045.   DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3046.   
  3047.   if (!INIT_FAILURE_STACK (failure_stack))
  3048.     return -2;
  3049.     
  3050.   if (!(regstart && regend && old_regstart && old_regend && reg_info 
  3051.         && best_regstart && best_regend)) 
  3052.     {
  3053.       FREE_VARIABLES ();
  3054.       return -2;
  3055.     }
  3056.  
  3057.   /* The starting position is bogus.  */
  3058.   if (pos < 0 || pos > size1 + size2)
  3059.     {
  3060.       FREE_VARIABLES ();
  3061.       return -1;
  3062.     }
  3063.     
  3064.   
  3065.   /* Initialize subexpression text positions to -1 to mark ones that no
  3066.      \( or ( and \) or ) has been seen for. Also set all registers to
  3067.      inactive and mark them as not having any inner groups, able to
  3068.      match the empty string, matched anything so far, or ever failed.  */
  3069.   for (mcnt = 0; mcnt < num_regs; mcnt++)
  3070.     {
  3071.       regstart[mcnt] = regend[mcnt] 
  3072.         = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3073.         
  3074.       REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NOTHING_UNSET_VALUE;
  3075.       IS_ACTIVE (reg_info[mcnt]) = 0;
  3076.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3077.       EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3078.     }
  3079.   
  3080.   IS_ACTIVE (reg_info[0]) = 1;
  3081.  
  3082.   /* We move string1 into string2 if the latter's empty---but not if
  3083.      string1 is null.  */
  3084.   if (size2 == 0 && string1 != NULL)
  3085.     {
  3086.       string2 = string1;
  3087.       size2 = size1;
  3088.       string1 = 0;
  3089.       size1 = 0;
  3090.     }
  3091.   end1 = string1 + size1;
  3092.   end2 = string2 + size2;
  3093.  
  3094.   /* Compute where to stop matching, within the two strings.  */
  3095.   if (stop <= size1)
  3096.     {
  3097.       end_match_1 = string1 + stop;
  3098.       end_match_2 = string2;
  3099.     }
  3100.   else
  3101.     {
  3102.       end_match_1 = end1;
  3103.       end_match_2 = string2 + stop - size1;
  3104.     }
  3105.  
  3106.   /* `p' scans through the pattern as `d' scans through the data.  `dend'
  3107.      is the end of the input string that `d' points within.  `d' is
  3108.      advanced into the following input string whenever necessary, but
  3109.      this happens before fetching; therefore, at the beginning of the
  3110.      loop, `d' can be pointing at the end of a string, but it cannot
  3111.      equal `string2'.  */
  3112.   if (size1 > 0 && pos <= size1)
  3113.     {
  3114.       d = string1 + pos;
  3115.       dend = end_match_1;
  3116.     }
  3117.   else
  3118.     {
  3119.       d = string2 + pos - size1;
  3120.       dend = end_match_2;
  3121.     }
  3122.  
  3123.   DEBUG_PRINT1 ("The compiled pattern is: ");
  3124.   DEBUG_COMPILED_PATTERN_PRINTER (bufp, p, pend);
  3125.   DEBUG_PRINT1 ("The string to match is: `");
  3126.   DEBUG_DOUBLE_STRING_PRINTER (d, string1, size1, string2, size2);
  3127.   DEBUG_PRINT1 ("'\n");
  3128.   
  3129.   /* This loops over pattern commands.  It exits by returning from the
  3130.      function if the match is complete, or it drops through if the match
  3131.      fails at this starting point in the input data.  */
  3132.   for (;;)
  3133.     {
  3134.       DEBUG_PRINT2 ("\n0x%x: ", p);
  3135.  
  3136.       if (p == pend)
  3137.     { /* End of pattern means we might have succeeded.  */
  3138.           DEBUG_PRINT1 ("End of pattern: ");
  3139.       /* If not end of string, try backtracking.  Otherwise done.  */
  3140.           if (d != end_match_2)
  3141.         {
  3142.               DEBUG_PRINT1 ("backtracking.\n");
  3143.               
  3144.               if (!FAILURE_STACK_EMPTY ())
  3145.                 { /* More failure points to try.  */
  3146.  
  3147.                   boolean in_same_string = 
  3148.                           IS_IN_FIRST_STRING (best_regend[0]) 
  3149.                         == MATCHING_IN_FIRST_STRING;
  3150.  
  3151.                   /* If exceeds best match so far, save it.  */
  3152.                   if (!best_regs_set
  3153.                       || (in_same_string && d > best_regend[0])
  3154.                       || (!in_same_string && !MATCHING_IN_FIRST_STRING))
  3155.                     {
  3156.                       best_regs_set = 1;
  3157.                       best_regend[0] = d;    /* Never use regstart[0].  */
  3158.                       
  3159.                       for (mcnt = 1; mcnt < num_regs; mcnt++)
  3160.                         {
  3161.                           best_regstart[mcnt] = regstart[mcnt];
  3162.                           best_regend[mcnt] = regend[mcnt];
  3163.                         }
  3164.                     }
  3165.                   goto fail;           
  3166.                 }
  3167.  
  3168.               /* If no failure points, don't restore garbage.  */
  3169.               else if (best_regs_set)   
  3170.                 {
  3171.               restore_best_regs:
  3172.                   /* Restore best match.  */
  3173.                   d = best_regend[0];
  3174.                   
  3175.                   if (d >= string1 && d <= end1)
  3176.                     dend = end_match_1;
  3177.  
  3178.           for (mcnt = 0; mcnt < num_regs; mcnt++)
  3179.             {
  3180.               regstart[mcnt] = best_regstart[mcnt];
  3181.               regend[mcnt] = best_regend[mcnt];
  3182.             }
  3183.                 }
  3184.             } /* d != end_match_2 */
  3185.  
  3186.           DEBUG_PRINT1 ("accepting match.\n");
  3187.  
  3188.           /* If caller wants register contents data back, do it.  */
  3189.           if (regs && !bufp->no_sub)
  3190.         {
  3191.               /* If they haven't allocated it, we'll do it.  */
  3192.               if (!bufp->caller_allocated_regs)
  3193.                 {
  3194.                   regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3195.                   regs->start = TALLOC (regs->num_regs, regoff_t);
  3196.                   regs->end = TALLOC (regs->num_regs, regoff_t);
  3197.                   if (regs->start == NULL || regs->end == NULL)
  3198.                     return -2;
  3199.                 }
  3200.               
  3201.               /* Convert the pointer data in `regstart' and `regend' to
  3202.                  indices.  Register zero has to be set differently,
  3203.                  since we haven't kept track of any info for it.  */
  3204.               if (regs->num_regs > 0)
  3205.                 {
  3206.                   regs->start[0] = pos;
  3207.                   regs->end[0] = MATCHING_IN_FIRST_STRING
  3208.                              ? d - string1
  3209.                      : d - string2 + size1;
  3210.                 }
  3211.               
  3212.               /* Go through the first min (num_regs, regs->num_regs)
  3213.                  registers, since that is all we initialized at the
  3214.                  beginning.  */
  3215.           for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
  3216.         {
  3217.                   if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  3218.                     regs->start[mcnt] = regs->end[mcnt] = -1;
  3219.                   else
  3220.                     {
  3221.               regs->start[mcnt] = POINTER_TO_OFFSET (regstart[mcnt]);
  3222.                       regs->end[mcnt] = POINTER_TO_OFFSET (regend[mcnt]);
  3223.                     }
  3224.         }
  3225.               
  3226.               /* If the regs structure we return has more elements than
  3227.                  it than were in the pattern, set the extra elements to
  3228.                  -1.  If we allocated the registers, this is the case,
  3229.                  because we always allocate enough to have at least -1
  3230.                  at the end.  */
  3231.               for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
  3232.                 regs->start[mcnt] = regs->end[mcnt] = -1;
  3233.         } /* regs && !bufp->no_sub */
  3234.  
  3235.           FREE_VARIABLES ();
  3236.           DEBUG_PRINT2 ("%d registers pushed.\n", num_regs_pushed);
  3237.  
  3238.           mcnt = d - pos - (MATCHING_IN_FIRST_STRING 
  3239.                 ? string1 
  3240.                 : string2 - size1);
  3241.  
  3242.           DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  3243.  
  3244.           return mcnt;
  3245.         }
  3246.  
  3247.       /* Otherwise match next pattern command.  */
  3248. #ifdef SWITCH_ENUM_BUG
  3249.       switch ((int) ((re_opcode_t) *p++))
  3250. #else
  3251.       switch ((re_opcode_t) *p++)
  3252. #endif
  3253.     {
  3254.         /* Ignore these.  Used to ignore the n of succeed_n's which
  3255.            currently have n == 0.  */
  3256.         case no_op:
  3257.           DEBUG_PRINT1 ("EXECUTING no_op.\n");
  3258.           break;
  3259.  
  3260.  
  3261.         /* Match the next n pattern characters exactly.  The following
  3262.            byte in the pattern defines n, and the n bytes after that
  3263.            are the characters to match.  */
  3264.     case exactn:
  3265.       mcnt = *p++;
  3266.           DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  3267.  
  3268.           /* This is written out as an if-else so we don't waste time
  3269.              testing `translate' inside the loop.  */
  3270.           if (translate)
  3271.         {
  3272.           do
  3273.         {
  3274.           PREFETCH;
  3275.           if (translate[(unsigned char) *d++] != (char) *p++)
  3276.                     goto fail;
  3277.         }
  3278.           while (--mcnt);
  3279.         }
  3280.       else
  3281.         {
  3282.           do
  3283.         {
  3284.           PREFETCH;
  3285.           if (*d++ != (char) *p++) goto fail;
  3286.         }
  3287.           while (--mcnt);
  3288.         }
  3289.       SET_REGS_MATCHED ();
  3290.           break;
  3291.  
  3292.  
  3293.         /* Match anything but possibly a newline or a null.  */
  3294.     case anychar:
  3295.           DEBUG_PRINT1 ("EXECUTING anychar.\n");
  3296.  
  3297.           PREFETCH;
  3298.  
  3299.           if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
  3300.               || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
  3301.         goto fail;
  3302.  
  3303.           SET_REGS_MATCHED ();
  3304.           d++;
  3305.       break;
  3306.  
  3307.  
  3308.     case charset:
  3309.     case charset_not:
  3310.       {
  3311.         register unsigned char c;
  3312.         boolean not = (re_opcode_t) *(p - 1) == charset_not;
  3313.  
  3314.             DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  3315.  
  3316.         PREFETCH;
  3317.         c = TRANSLATE (*d); /* The character to match.  */
  3318.  
  3319.         if (c < (unsigned char) (*p * BYTEWIDTH)
  3320.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3321.           not = !not;
  3322.  
  3323.         p += 1 + *p;
  3324.  
  3325.         if (!not) goto fail;
  3326.             
  3327.         SET_REGS_MATCHED ();
  3328.             d++;
  3329.         break;
  3330.       }
  3331.  
  3332.  
  3333.         /* The beginning of a group is represented by start_memory.
  3334.            The arguments are the register number in the next byte, and the
  3335.            number of groups inner to this one in the next.  The text
  3336.            matched within the group is recorded (in the internal
  3337.            registers data structure) under the register number.  */
  3338.         case start_memory:
  3339.       DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  3340.  
  3341.           /* Find out if this group can match the empty string.  */
  3342.       p1 = p;        /* To send to group_match_null_string_p.  */
  3343.           
  3344.           if (REG_MATCH_NULL_STRING_P (reg_info[*p])
  3345.               == MATCH_NOTHING_UNSET_VALUE)
  3346.             REG_MATCH_NULL_STRING_P (reg_info[*p]) 
  3347.               = group_match_null_string_p (&p1, pend, reg_info);
  3348.  
  3349.           /* Save the position in the string where we were the last time
  3350.              we were at this open-group operator in case the group is
  3351.              operated upon by a repetition operator, e.g., with `(a*)*b'
  3352.              against `ab'; then we want to ignore where we are now in
  3353.              the string in case this attempt to match fails.  */
  3354.           old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3355.                              ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  3356.                              : regstart[*p];
  3357.       DEBUG_PRINT2 ("  old_regstart: %d\n", 
  3358.              POINTER_TO_OFFSET (old_regstart[*p]));
  3359.  
  3360.           regstart[*p] = d;
  3361.       DEBUG_PRINT2 ("  regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  3362.  
  3363.           IS_ACTIVE (reg_info[*p]) = 1;
  3364.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  3365.           
  3366.           /* This is the new highest active register.  */
  3367.           highest_active_reg = *p;
  3368.           
  3369.           /* If nothing was active before, this is the new lowest active
  3370.              register.  */
  3371.           if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3372.             lowest_active_reg = *p;
  3373.  
  3374.           /* Move past the register number and inner group count.  */
  3375.           p += 2;
  3376.           break;
  3377.  
  3378.  
  3379.         /* The stop_memory opcode represents the end of a group.  Its
  3380.            arguments are the same as start_memory's: the register
  3381.            number, and the number of inner groups.  */
  3382.     case stop_memory:
  3383.       DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  3384.              
  3385.           /* We need to save the string position the last time we were at
  3386.              this close-group operator in case the group is operated
  3387.              upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  3388.              against `aba'; then we want to ignore where we are now in
  3389.              the string in case this attempt to match fails.  */
  3390.           old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3391.                            ? REG_UNSET (regend[*p]) ? d : regend[*p]
  3392.                : regend[*p];
  3393.       DEBUG_PRINT2 ("      old_regend: %d\n", 
  3394.              POINTER_TO_OFFSET (old_regend[*p]));
  3395.  
  3396.           regend[*p] = d;
  3397.       DEBUG_PRINT2 ("      regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  3398.  
  3399.           /* This register isn't active anymore.  */
  3400.           IS_ACTIVE (reg_info[*p]) = 0;
  3401.           
  3402.           /* If this was the only register active, nothing is active
  3403.              anymore.  */
  3404.           if (lowest_active_reg == highest_active_reg)
  3405.             {
  3406.               lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3407.               highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3408.             }
  3409.           else
  3410.             { /* We must scan for the new highest active register, since
  3411.                  it isn't necessarily one less than now: consider
  3412.                  (a(b)c(d(e)f)g).  When group 3 ends, after the f), the
  3413.                  new highest active register is 1.  */
  3414.               unsigned char r = *p - 1;
  3415.               
  3416.               /* This loop will always terminate, because register 0 is
  3417.                  always active.  */
  3418.           assert (IS_ACTIVE (reg_info[0]));
  3419.               while (!IS_ACTIVE (reg_info[r]))
  3420.                 r--;
  3421.               
  3422.               /* If we end up at register zero, that means that we saved
  3423.                  the registers as the result of an on_failure_jump, not
  3424.                  a start_memory, and we jumped to past the innermost
  3425.                  stop_memory.  For example, in ((.)*).  We save
  3426.                  registers 1 and 2 as a result of the *, but when we pop
  3427.                  back to the second ), we are at the stop_memory 1.
  3428.                  Thus, nothing is active.  */
  3429.           if (r != 0)
  3430.                 highest_active_reg = r;
  3431.               else
  3432.                 {
  3433.                   lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3434.                   highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3435.                 }
  3436.             }
  3437.           
  3438.           /* If just failed to match something this time around with a
  3439.              group that's operated on by a repetition operator, try to
  3440.              force exit from the ``loop,'' and restore the register
  3441.              information for this group that we had before trying this
  3442.              last match.  */
  3443.           if ((!MATCHED_SOMETHING (reg_info[*p])
  3444.                || (re_opcode_t) p[-3] == start_memory)
  3445.           && (p + 2) < pend)              
  3446.             {
  3447.               boolean is_a_jump_n = false;
  3448.               
  3449.               p1 = p + 2;
  3450.               mcnt = 0;
  3451.               switch ((re_opcode_t) *p1++)
  3452.                 {
  3453.                   case no_pop_jump_n:
  3454.             is_a_jump_n = true;
  3455.                   case pop_failure_jump:
  3456.           case maybe_pop_jump:
  3457.           case no_pop_jump:
  3458.           case dummy_failure_jump:
  3459.                     EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3460.             if (is_a_jump_n)
  3461.               p1 += 2;
  3462.                     break;
  3463.                   
  3464.                   default:
  3465.                     /* do nothing */ ;
  3466.                 }
  3467.           p1 += mcnt;
  3468.         
  3469.               /* If the next operation is a jump backwards in the pattern
  3470.              to an on_failure_jump right before the start_memory
  3471.                  corresponding to this stop_memory, exit from the loop
  3472.                  by forcing a failure after pushing on the stack the
  3473.                  on_failure_jump's jump in the pattern, and d.  */
  3474.               if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  3475.                   && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  3476.         {
  3477.                   /* If this group ever matched anything, then restore
  3478.                      what its registers were before trying this last
  3479.                      failed match, e.g., with `(a*)*b' against `ab' for
  3480.                      regstart[1], and, e.g., with `((a*)*(b*)*)*'
  3481.                      against `aba' for regend[3].
  3482.                      
  3483.                      Also restore the registers for inner groups for,
  3484.                      e.g., `((a*)(b*))*' against `aba' (register 3 would
  3485.                      otherwise get trashed).  */
  3486.                      
  3487.                   if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  3488.             {
  3489.               unsigned r; 
  3490.         
  3491.                       EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  3492.                       
  3493.               /* Restore this and inner groups' (if any) registers.  */
  3494.                       for (r = *p; r < *p + *(p + 1); r++)
  3495.                         {
  3496.                           regstart[r] = old_regstart[r];
  3497.  
  3498.                           /* xx why this test?  */
  3499.                           if ((int) old_regend[r] >= (int) regstart[r])
  3500.                             regend[r] = old_regend[r];
  3501.                         }     
  3502.                     }
  3503.           p1++;
  3504.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3505.                   PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  3506.  
  3507.                   goto fail;
  3508.                 }
  3509.             }
  3510.           
  3511.           /* Move past the register number and the inner group count.  */
  3512.           p += 2;
  3513.           break;
  3514.  
  3515.  
  3516.     /* \<digit> has been turned into a `duplicate' command which is
  3517.            followed by the numeric value of <digit> as the register number.  */
  3518.         case duplicate:
  3519.       {
  3520.         register const char *d2, *dend2;
  3521.         int regno = *p++;   /* Get which register to match against.  */
  3522.         DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  3523.  
  3524.         /* Can't back reference a group which we've never matched.  */
  3525.             if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  3526.               goto fail;
  3527.               
  3528.             /* Where in input to try to start matching.  */
  3529.             d2 = regstart[regno];
  3530.             
  3531.             /* Where to stop matching; if both the place to start and
  3532.                the place to stop matching are in the same string, then
  3533.                set to the place to stop, otherwise, for now have to use
  3534.                the end of the first string.  */
  3535.  
  3536.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  3537.               == IS_IN_FIRST_STRING (regend[regno]))
  3538.              ? regend[regno] : end_match_1);
  3539.         for (;;)
  3540.           {
  3541.         /* If necessary, advance to next segment in register
  3542.                    contents.  */
  3543.         while (d2 == dend2)
  3544.           {
  3545.             if (dend2 == end_match_2) break;
  3546.             if (dend2 == regend[regno]) break;
  3547.  
  3548.                     /* End of string1 => advance to string2. */
  3549.                     d2 = string2;
  3550.                     dend2 = regend[regno];
  3551.           }
  3552.         /* At end of register contents => success */
  3553.         if (d2 == dend2) break;
  3554.  
  3555.         /* If necessary, advance to next segment in data.  */
  3556.         PREFETCH;
  3557.  
  3558.         /* How many characters left in this segment to match.  */
  3559.         mcnt = dend - d;
  3560.                 
  3561.         /* Want how many consecutive characters we can match in
  3562.                    one shot, so, if necessary, adjust the count.  */
  3563.                 if (mcnt > dend2 - d2)
  3564.           mcnt = dend2 - d2;
  3565.                   
  3566.         /* Compare that many; failure if mismatch, else move
  3567.                    past them.  */
  3568.         if (translate 
  3569.                     ? bcmp_translate (d, d2, mcnt, translate) 
  3570.                     : bcmp (d, d2, mcnt))
  3571.           goto fail;
  3572.         d += mcnt, d2 += mcnt;
  3573.           }
  3574.       }
  3575.       break;
  3576.  
  3577.  
  3578.         /* begline matches the empty string at the beginning of the string
  3579.            (unless `not_bol' is set in `bufp'), and, if
  3580.            `newline_anchor' is set, after newlines.  */
  3581.     case begline:
  3582.           DEBUG_PRINT1 ("EXECUTING begline.\n");
  3583.           
  3584.           if (AT_STRINGS_BEG)
  3585.             {
  3586.               if (!bufp->not_bol) break;
  3587.             }
  3588.           else if (d[-1] == '\n'  && bufp->newline_anchor)
  3589.             {
  3590.               break;
  3591.             }
  3592.           /* In all other cases, we fail.  */
  3593.           goto fail;
  3594.  
  3595.  
  3596.         /* endline is the dual of begline.  */
  3597.     case endline:
  3598.           DEBUG_PRINT1 ("EXECUTING endline.\n");
  3599.  
  3600.           if (AT_STRINGS_END)
  3601.             {
  3602.               if (!bufp->not_eol) break;
  3603.             }
  3604.           
  3605.           /* We have to ``prefetch'' the next character.  */
  3606.           else if ((d == end1 ? *string2 : *d) == '\n'
  3607.                    && bufp->newline_anchor)
  3608.             {
  3609.               break;
  3610.             }
  3611.           goto fail;
  3612.  
  3613.  
  3614.     /* Match at the very beginning of the data.  */
  3615.         case begbuf:
  3616.           DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  3617.           if (AT_STRINGS_BEG)
  3618.             break;
  3619.           goto fail;
  3620.  
  3621.  
  3622.     /* Match at the very end of the data.  */
  3623.         case endbuf:
  3624.           DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  3625.       if (AT_STRINGS_END)
  3626.         break;
  3627.           goto fail;
  3628.  
  3629.  
  3630.         /* on_failure_keep_string_jump is used to optimize `.*\n'.  It
  3631.            pushes NULL as the value for the string on the stack.  Then
  3632.            pop_failure_point will keep the current value for the string,
  3633.            instead of restoring it.  To see why, consider matching
  3634.            `foo\nbar' against `.*\n'.  The .* matches the foo; then the
  3635.            . fails against the \n.  But the next thing we want to do is
  3636.            match the \n against the \n; if we restored the string value,
  3637.            we would be back at the foo.
  3638.            
  3639.            Because this is used only in specific cases, we don't need to
  3640.            go through the hassle of checking all the things that
  3641.            on_failure_jump does, to make sure the right things get saved
  3642.            on the stack.  Hence we don't share its code.  The only
  3643.            reason to push anything on the stack at all is that otherwise
  3644.            we would have to change anychar's code to do something
  3645.            besides goto fail in this case; that seems worse than this.  */
  3646.         case on_failure_keep_string_jump:
  3647.           DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  3648.           
  3649.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3650.           DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  3651.  
  3652.           PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  3653.           break;
  3654.  
  3655.  
  3656.     /* Uses of on_failure_jump:
  3657.         
  3658.            Each alternative starts with an on_failure_jump that points
  3659.            to the beginning of the next alternative.  Each alternative
  3660.            except the last ends with a jump that in effect jumps past
  3661.            the rest of the alternatives.  (They really jump to the
  3662.            ending jump of the following alternative, because tensioning
  3663.            these jumps is a hassle.)
  3664.  
  3665.            Repeats start with an on_failure_jump that points past both
  3666.            the repetition text and either the following jump or
  3667.            pop_failure_jump back to this on_failure_jump.  */
  3668.     case on_failure_jump:
  3669.         on_failure:
  3670.           DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  3671.  
  3672.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3673.           DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  3674.  
  3675.           /* If this on_failure_jump comes right before a group (i.e.,
  3676.              the original * applied to a group), save the information
  3677.              for that group and all inner ones, so that if we fail back
  3678.              to this point, the group's information will be correct.
  3679.              For example, in \(a*\)*\1, we only need the preceding group,
  3680.              and in \(\(a*\)b*\)\2, we need the inner group.  */
  3681.  
  3682.           /* We can't use `p' to check ahead because we push
  3683.              a failure point to `p + mcnt' after we do this.  */
  3684.           p1 = p;
  3685.  
  3686.           /* We need to skip no_op's before we look for the
  3687.              start_memory in case this on_failure_jump is happening as
  3688.              the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  3689.              against aba.  */
  3690.           while (p1 < pend && (re_opcode_t) *p1 == no_op)
  3691.             p1++;
  3692.  
  3693.           if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  3694.             {
  3695.               /* We have a new highest active register now.  This will
  3696.                  get reset at the start_memory we are about to get to,
  3697.                  but we will have saved all the registers relevant to
  3698.                  this repetition op, as described above.  */
  3699.               highest_active_reg = *(p1 + 1) + *(p1 + 2);
  3700.               if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3701.                 lowest_active_reg = *(p1 + 1);
  3702.             }
  3703.  
  3704.           DEBUG_PRINT1 (":\n");
  3705.           PUSH_FAILURE_POINT (p + mcnt, d, -2);
  3706.           break;
  3707.  
  3708.  
  3709.         /* A smart repeat ends with a maybe_pop_jump.
  3710.        We change it either to a pop_failure_jump or a no_pop_jump.  */
  3711.         case maybe_pop_jump:
  3712.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3713.           DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  3714.           {
  3715.         register unsigned char *p2 = p;
  3716.  
  3717.             /* Compare the beginning of the repeat with what in the
  3718.                pattern follows its end. If we can establish that there
  3719.                is nothing that they would both match, i.e., that we
  3720.                would have to backtrack because of (as in, e.g., `a*a')
  3721.                then we can change to pop_failure_jump, because we'll
  3722.                never have to backtrack.  */
  3723.  
  3724.         /* Skip over open/close-group commands.  */
  3725.         while (p2 + 2 < pend
  3726.            && ((re_opcode_t) *p2 == stop_memory
  3727.                || (re_opcode_t) *p2 == start_memory))
  3728.           p2 += 3;            /* Skip over args, too.  */
  3729.  
  3730.             /* If we're at the end of the pattern, we can change.  */
  3731.             if (p2 == pend)
  3732.           p[-3] = (unsigned char) pop_failure_jump;
  3733.  
  3734.             else if ((re_opcode_t) *p2 == exactn
  3735.              || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  3736.           {
  3737.         register unsigned char c
  3738.                   = *p2 == (unsigned char) endline ? '\n' : p2[2];
  3739.         p1 = p + mcnt;
  3740.  
  3741.                 /* p1[0] ... p1[2] are the on_failure_jump corresponding
  3742.                    to the maybe_finalize_jump of this case. Examine what 
  3743.                    follows it.  */
  3744.                 if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
  3745.           p[-3] = (unsigned char) pop_failure_jump;
  3746.         else if ((re_opcode_t) p1[3] == charset
  3747.              || (re_opcode_t) p1[3] == charset_not)
  3748.           {
  3749.             int not = (re_opcode_t) p1[3] == charset_not;
  3750.                     
  3751.             if (c < (unsigned char) (p1[4] * BYTEWIDTH)
  3752.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3753.               not = !not;
  3754.  
  3755.                     /* `not' is equal to 1 if c would match, which means
  3756.                         that we can't change to pop_failure_jump.  */
  3757.             if (!not)
  3758.               p[-3] = (unsigned char) pop_failure_jump;
  3759.           }
  3760.           }
  3761.       }
  3762.       p -= 2;        /* Point at relative address again.  */
  3763.       if ((re_opcode_t) p[-1] != pop_failure_jump)
  3764.         {
  3765.           p[-1] = (unsigned char) no_pop_jump;
  3766.           goto no_pop;
  3767.         }
  3768.         /* Note fall through.  */
  3769.  
  3770.  
  3771.     /* The end of a simple repeat has a pop_failure_jump back to
  3772.            its matching on_failure_jump, where the latter will push a
  3773.            failure point.  The pop_failure_jump takes off failure
  3774.            points put on by this pop_failure_jump's matching
  3775.            on_failure_jump; we got through the pattern to here from the
  3776.            matching on_failure_jump, so didn't fail.  */
  3777.         case pop_failure_jump:
  3778.           {
  3779.             /* We need to pass separate storage for the lowest and
  3780.                highest registers, even though we aren't interested.
  3781.                Otherwise, we will restore only one register from the
  3782.                stack, since lowest will equal highest in
  3783.                pop_failure_point (since they'll be the same memory
  3784.                location).  */
  3785.             unsigned dummy_low, dummy_high;
  3786.             unsigned char *pdummy = NULL;
  3787.  
  3788.             DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  3789.             pop_failure_point (bufp, pend, 
  3790. #ifdef DEBUG
  3791.                    string1, size1, string2, size2,
  3792. #endif
  3793.                                &failure_stack, &pdummy, &pdummy,
  3794.                                &dummy_low, &dummy_high,
  3795.                                ®_dummy, ®_dummy, ®_info_dummy);
  3796.           }
  3797.           /* Note fall through.  */
  3798.  
  3799.           
  3800.         /* Jump without taking off any failure points.  */
  3801.         case no_pop_jump:
  3802.     no_pop:
  3803.       EXTRACT_NUMBER_AND_INCR (mcnt, p);    /* Get the amount to jump.  */
  3804.           DEBUG_PRINT2 ("EXECUTING no_pop_jump %d ", mcnt);
  3805.       p += mcnt;                /* Do the jump.  */
  3806.           DEBUG_PRINT2 ("(to 0x%x).\n", p);
  3807.       break;
  3808.  
  3809.     
  3810.         /* We need this opcode so we can detect where alternatives end
  3811.            in `group_match_null_string_p' et al.  */
  3812.         case jump_past_next_alt:
  3813.           DEBUG_PRINT1 ("EXECUTING jump_past_next_alt.\n");
  3814.           goto no_pop;
  3815.  
  3816.  
  3817.         /* Normally, the on_failure_jump pushes a failure point, which
  3818.            then gets popped at pop_failure_jump.  We will end up at
  3819.            pop_failure_jump, also, and with a pattern of, say, `a+', we
  3820.            are skipping over the on_failure_jump, so we have to push
  3821.            something meaningless for pop_failure_jump to pop.  */
  3822.         case dummy_failure_jump:
  3823.           DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  3824.           /* It doesn't matter what we push for the string here.  What
  3825.              the code at `fail' tests is the value for the pattern.  */
  3826.           PUSH_FAILURE_POINT (0, 0, -2);
  3827.           goto no_pop;
  3828.  
  3829.  
  3830.         /* Have to succeed matching what follows at least n times.  Then
  3831.            just handle like an on_failure_jump.  */
  3832.         case succeed_n: 
  3833.           EXTRACT_NUMBER (mcnt, p + 2);
  3834.           DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  3835.  
  3836.           /* Originally, this is how many times we HAVE to succeed.  */
  3837.           if (mcnt)
  3838.             {
  3839.                mcnt--;
  3840.            p += 2;
  3841.                STORE_NUMBER_AND_INCR (p, mcnt);
  3842.                DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p, mcnt);
  3843.             }
  3844.       else if (mcnt == 0)
  3845.             {
  3846.               DEBUG_PRINT2 ("  Setting two bytes from 0x%x to no_op.\n", p+2);
  3847.           p[2] = (unsigned char) no_op;
  3848.               p[3] = (unsigned char) no_op;
  3849.               goto on_failure;
  3850.             }
  3851. #ifdef DEBUG
  3852.           else
  3853.         { 
  3854.               fprintf (stderr, "regex: negative n at succeed_n.\n");
  3855.               abort ();
  3856.         }
  3857. #endif /* DEBUG */
  3858.           break;
  3859.         
  3860.         case no_pop_jump_n: 
  3861.           EXTRACT_NUMBER (mcnt, p + 2);
  3862.           DEBUG_PRINT2 ("EXECUTING no_pop_jump_n %d.\n", mcnt);
  3863.  
  3864.           /* Originally, this is how many times we CAN jump.  */
  3865.           if (mcnt)
  3866.             {
  3867.                mcnt--;
  3868.                STORE_NUMBER(p + 2, mcnt);
  3869.            goto no_pop;         
  3870.             }
  3871.           /* If don't have to jump any more, skip over the rest of command.  */
  3872.       else      
  3873.         p += 4;             
  3874.           break;
  3875.         
  3876.     case set_number_at:
  3877.       {
  3878.             DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  3879.  
  3880.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3881.             p1 = p + mcnt;
  3882.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3883.         STORE_NUMBER (p1, mcnt);
  3884.             break;
  3885.           }
  3886.  
  3887.         case wordbound:
  3888.           DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  3889.           if (AT_WORD_BOUNDARY (d))
  3890.         break;
  3891.           goto fail;
  3892.  
  3893.     case notwordbound:
  3894.           DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  3895.       if (AT_WORD_BOUNDARY (d))
  3896.         goto fail;
  3897.           break;
  3898.  
  3899.     case wordbeg:
  3900.           DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  3901.       if (LETTER_P (d) && (AT_STRINGS_BEG || !LETTER_P (d - 1)))
  3902.         break;
  3903.           goto fail;
  3904.  
  3905.     case wordend:
  3906.           DEBUG_PRINT1 ("EXECUTING wordend.\n");
  3907.       if (!AT_STRINGS_BEG && LETTER_P (d - 1)
  3908.               && (!LETTER_P (d) || AT_STRINGS_END))
  3909.         break;
  3910.           goto fail;
  3911.  
  3912. #ifdef emacs
  3913. #ifdef emacs19
  3914.       case before_dot:
  3915.           DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  3916.        if (PTR_CHAR_POS ((unsigned char *) d) >= point)
  3917.           goto fail;
  3918.         break;
  3919.   
  3920.       case at_dot:
  3921.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  3922.        if (PTR_CHAR_POS ((unsigned char *) d) != point)
  3923.           goto fail;
  3924.         break;
  3925.   
  3926.       case after_dot:
  3927.           DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  3928.           if (PTR_CHAR_POS ((unsigned char *) d) <= point)
  3929.           goto fail;
  3930.         break;
  3931. #else /* not emacs19 */
  3932.     case at_dot:
  3933.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  3934.       if (PTR_CHAR_POS ((unsigned char *) d) + 1 != point)
  3935.         goto fail;
  3936.       break;
  3937. #endif /* not emacs19 */
  3938.  
  3939.     case syntaxspec:
  3940.           DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  3941.       mcnt = *p++;
  3942.       goto matchsyntax;
  3943.  
  3944.         case wordchar:
  3945.           DEBUG_PRINT1 ("EXECUTING wordchar.\n");
  3946.       mcnt = (int) Sword;
  3947.         matchsyntax:
  3948.       PREFETCH;
  3949.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  3950.           SET_REGS_MATCHED ();
  3951.       break;
  3952.  
  3953.     case notsyntaxspec:
  3954.           DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  3955.       mcnt = *p++;
  3956.       goto matchnotsyntax;
  3957.  
  3958.         case notwordchar:
  3959.           DEBUG_PRINT1 ("EXECUTING notwordchar.\n");
  3960.       mcnt = (int) Sword;
  3961.         matchnotsyntax: /* We goto here from notsyntaxspec.  */
  3962.       PREFETCH;
  3963.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  3964.       SET_REGS_MATCHED ();
  3965.           break;
  3966.  
  3967. #else /* not emacs */
  3968.     case wordchar:
  3969.           DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  3970.       PREFETCH;
  3971.           if (!LETTER_P (d))
  3972.             goto fail;
  3973.       SET_REGS_MATCHED ();
  3974.       break;
  3975.       
  3976.     case notwordchar:
  3977.           DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  3978.       PREFETCH;
  3979.       if (LETTER_P (d))
  3980.             goto fail;
  3981.           SET_REGS_MATCHED ();
  3982.       break;
  3983. #endif /* not emacs */
  3984.           
  3985.         default:
  3986.           abort ();
  3987.     }
  3988.       continue;  /* Successfully executed one pattern command; keep going.  */
  3989.  
  3990.  
  3991.     /* We goto here if a matching operation fails. */
  3992.     fail:
  3993.       if (!FAILURE_STACK_EMPTY ())
  3994.     { /* A restart point is known.  Restore to that state.  */
  3995.           DEBUG_PRINT1 ("\nFAIL:\n");
  3996.           pop_failure_point (bufp, pend,
  3997. #ifdef DEBUG
  3998.                        string1, size1, string2, size2,
  3999. #endif
  4000.                              &failure_stack, &p, &d, &lowest_active_reg,
  4001.                              &highest_active_reg, ®start, ®end,
  4002.                              ®_info);
  4003.  
  4004.           /* If this failure point is a dummy, try the next one.  */
  4005.           if (!p)
  4006.         goto fail;
  4007.  
  4008.           /* If we failed to the end of the pattern, don't examine *p.  */
  4009.       assert (p <= pend);
  4010.           if (p < pend)
  4011.             {
  4012.               boolean is_a_jump_n = false;
  4013.               
  4014.               /* If failed to a backwards jump that's part of a repetition
  4015.                  loop, need to pop this failure point and use the next one.  */
  4016.               switch ((re_opcode_t) *p)
  4017.                 {
  4018.                 case no_pop_jump_n:
  4019.                   is_a_jump_n = true;
  4020.                 case maybe_pop_jump:
  4021.                 case pop_failure_jump:
  4022.                 case no_pop_jump:
  4023.                   p1 = p + 1;
  4024.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4025.                   p1 += mcnt;    
  4026.  
  4027.                   if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4028.                       || (!is_a_jump_n
  4029.                           && (re_opcode_t) *p1 == on_failure_jump))
  4030.                     goto fail;
  4031.                   break;
  4032.                 default:
  4033.                   /* do nothing */ ;
  4034.                 }
  4035.             }
  4036.  
  4037.           if (d >= string1 && d <= end1)
  4038.         dend = end_match_1;
  4039.         }
  4040.       else
  4041.         break;   /* Matching at this starting point really fails.  */
  4042.     } /* for (;;) */
  4043.  
  4044.   if (best_regs_set)
  4045.     goto restore_best_regs;
  4046.  
  4047.   FREE_VARIABLES ();
  4048.  
  4049.   return -1;                     /* Failure to match.  */
  4050. } /* re_match_2 */
  4051.  
  4052. /* Subroutine definitions for re_match_2.  */
  4053.  
  4054.  
  4055. /* Pops what PUSH_FAILURE_STACK pushes.  */
  4056.  
  4057. static void 
  4058. pop_failure_point (bufp, pattern_end, 
  4059. #ifdef DEBUG
  4060.                    string1, size1, string2, size2,
  4061. #endif
  4062.                    failure_stack_ptr, pattern_place, string_place, 
  4063.                    lowest_active_reg, highest_active_reg,
  4064.                    regstart, regend, reg_info)
  4065.     const struct re_pattern_buffer *bufp;      /* These not modified.  */
  4066.     unsigned char *pattern_end;
  4067. #ifdef DEBUG
  4068.     unsigned char *string1, *string2;
  4069.     int size1, size2;
  4070. #endif
  4071.     failure_stack_type *failure_stack_ptr;    /* These get modified.  */
  4072.     const unsigned char **pattern_place;
  4073.     const unsigned char **string_place;
  4074.     unsigned *lowest_active_reg, *highest_active_reg;
  4075.     const unsigned char ***regstart;
  4076.     const unsigned char ***regend;
  4077.     register_info_type **reg_info;
  4078. {                                    
  4079. #ifdef DEBUG
  4080.   /* Type is really unsigned; it's declared this way just to avoid a
  4081.      compiler warning.  */
  4082.   failure_stack_elt_t failure_id;
  4083. #endif
  4084.   int this_reg;
  4085.   const unsigned char *string_temp;
  4086.  
  4087.   assert (!FAILURE_STACK_PTR_EMPTY ());
  4088.  
  4089.   /* Remove failure points and point to how many regs pushed.  */
  4090.   DEBUG_PRINT1 ("pop_failure_point:\n");
  4091.   DEBUG_PRINT2 ("  Before pop, next avail: %d\n", failure_stack_ptr->avail); 
  4092.   DEBUG_PRINT2 ("                    size: %d\n", failure_stack_ptr->size);
  4093.  
  4094.   assert (failure_stack_ptr->avail >= NUM_NONREG_ITEMS);
  4095.  
  4096.   DEBUG_POP (&failure_id);
  4097.   DEBUG_PRINT2 ("  Popping failure id: %u\n", failure_id);
  4098.   
  4099.   /* If the saved string location is NULL, it came from an
  4100.      on_failure_keep_string_jump opcode, and we want to throw away the
  4101.      saved NULL, thus retaining our current position in the string.  */
  4102.   string_temp = POP_FAILURE_ITEM ();
  4103.   if (string_temp != NULL)
  4104.     *string_place = string_temp;
  4105.     
  4106.   DEBUG_PRINT2 ("  Popping string 0x%x: `", *string_place);
  4107.   DEBUG_DOUBLE_STRING_PRINTER (*string_place, string1, size1, string2, size2);
  4108.   DEBUG_PRINT1 ("'\n");
  4109.   
  4110.   *pattern_place = POP_FAILURE_ITEM ();
  4111.   DEBUG_PRINT2 ("  Popping pattern 0x%x: ", *pattern_place);
  4112.   DEBUG_COMPILED_PATTERN_PRINTER (bufp, *pattern_place, pattern_end);
  4113.  
  4114.   /* Restore register info.  */
  4115.   *highest_active_reg = (unsigned) POP_FAILURE_ITEM ();
  4116.   DEBUG_PRINT2 ("  Popping high active reg: %d\n", *highest_active_reg);
  4117.  
  4118.   *lowest_active_reg = (unsigned) POP_FAILURE_ITEM ();
  4119.   DEBUG_PRINT2 ("  Popping low active reg: %d\n", *lowest_active_reg);
  4120.  
  4121.   for (this_reg = *highest_active_reg; this_reg >= *lowest_active_reg; 
  4122.        this_reg--)
  4123.     {
  4124.       DEBUG_PRINT2 ("    Popping reg: %d\n", this_reg);
  4125.  
  4126.       (*reg_info)[this_reg].word = POP_FAILURE_ITEM ();
  4127.       DEBUG_PRINT2 ("      info: 0x%x\n", (*reg_info)[this_reg]);
  4128.  
  4129.       (*regend)[this_reg] = POP_FAILURE_ITEM ();
  4130.       DEBUG_PRINT2 ("      end: 0x%x\n", (*regend)[this_reg]);
  4131.  
  4132.       (*regstart)[this_reg] = POP_FAILURE_ITEM ();
  4133.       DEBUG_PRINT2 ("      start: 0x%x\n", (*regstart)[this_reg]);
  4134.     }
  4135. }  /* pop_failure_point */
  4136.  
  4137.  
  4138. /* We are passed P pointing to a register number after a start_memory.
  4139.    
  4140.    Return true if the pattern up to the corresponding stop_memory can
  4141.    match the empty string, and false otherwise.
  4142.    
  4143.    If we find the matching stop_memory, sets P to point to one past its number.
  4144.    Otherwise, sets P to an undefined byte less than or equal to END.
  4145.  
  4146.    We don't handle duplicates properly (yet).  */
  4147.  
  4148. static boolean
  4149. group_match_null_string_p (p, end, reg_info)
  4150.     unsigned char **p, *end;
  4151.     register_info_type *reg_info;
  4152. {
  4153.   int mcnt;
  4154.   /* Point to after the args to the start_memory.  */
  4155.   unsigned char *p1 = *p + 2;
  4156.   
  4157.   while (p1 < end)
  4158.     {
  4159.       /* Skip over opcodes that can match nothing, and return true or
  4160.      false, as appropriate, when we get to one that can't, or to the
  4161.          matching stop_memory.  */
  4162.       
  4163.       switch ((re_opcode_t) *p1)
  4164.         {
  4165.         /* Could be either a loop or a series of alternatives.  */
  4166.         case on_failure_jump:
  4167.           p1++;
  4168.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4169.           
  4170.           /* If the next operation is not a jump backwards in the
  4171.          pattern.  */
  4172.  
  4173.       if (mcnt >= 0)
  4174.         {
  4175.               /* Go through the on_failure_jumps of the alternatives,
  4176.                  seeing if any of the alternatives cannot match nothing.
  4177.                  The last alternative starts with only a no_pop_jump,
  4178.                  whereas the rest start with on_failure_jump and end
  4179.                  with a no_pop_jump, e.g., here is the pattern for `a|b|c':
  4180.  
  4181.                  /on_failure_jump/0/6/exactn/1/a/jump_past_next_alt/0/6
  4182.                  /on_failure_jump/0/6/exactn/1/b/jump_past_next_alt/0/3
  4183.                  /exactn/1/c                        
  4184.  
  4185.                  So, we have to first go through the first (n-1)
  4186.                  alternatives and then deal with the last one separately.  */
  4187.  
  4188.  
  4189.               /* Deal with the first (n-1) alternatives, which start
  4190.                  with an on_failure_jump (see above) that jumps to right
  4191.                  past a jump_past_next_alt.  */
  4192.  
  4193.               while ((re_opcode_t) p1[mcnt-3] == jump_past_next_alt)
  4194.                 {
  4195.                   /* `mcnt' holds how many bytes long the alternative
  4196.                      is, including the ending `jump_past_next_alt' and
  4197.                      its number.  */
  4198.  
  4199.                   if (!alt_match_null_string_p (p1, p1 + mcnt - 3, 
  4200.                                       reg_info))
  4201.                     return false;
  4202.  
  4203.                   /* Move to right after this alternative, including the
  4204.              jump_past_next_alt.  */
  4205.                   p1 += mcnt;    
  4206.  
  4207.                   /* Break if it's the beginning of an n-th alternative
  4208.                      that doesn't begin with an on_failure_jump.  */
  4209.                   if ((re_opcode_t) *p1 != on_failure_jump)
  4210.                     break;
  4211.         
  4212.           /* Still have to check that it's not an n-th
  4213.              alternative that starts with an on_failure_jump.  */
  4214.           p1++;
  4215.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4216.                   if ((re_opcode_t) p1[mcnt-3] != jump_past_next_alt)
  4217.                     {
  4218.               /* Get to the beginning of the n-th alternative.  */
  4219.                       p1 -= 3;
  4220.                       break;
  4221.                     }
  4222.                 }
  4223.  
  4224.               /* Deal with the last alternative: go back and get number
  4225.                  of the jump_past_next_alt just before it.  `mcnt'
  4226.                  contains how many bytes long the alternative is.  */
  4227.               EXTRACT_NUMBER (mcnt, p1 - 2);
  4228.  
  4229.               if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  4230.                 return false;
  4231.  
  4232.               p1 += mcnt;    /* Get past the n-th alternative.  */
  4233.             } /* if mcnt > 0 */
  4234.           break;
  4235.  
  4236.           
  4237.         case stop_memory:
  4238.       assert (p1[1] == **p);
  4239.           *p = p1 + 2;
  4240.           return true;
  4241.  
  4242.         
  4243.         default: 
  4244.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  4245.             return false;
  4246.         }
  4247.     } /* while p1 < end */
  4248.  
  4249.   return false;
  4250. } /* group_match_null_string_p */
  4251.  
  4252.  
  4253. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  4254.    It expects P to be the first byte of a single alternative and END one
  4255.    byte past the last. The alternative can contain groups.  */
  4256.    
  4257. static boolean
  4258. alt_match_null_string_p (p, end, reg_info)
  4259.     unsigned char *p, *end;
  4260.     register_info_type *reg_info;
  4261. {
  4262.   int mcnt;
  4263.   unsigned char *p1 = p;
  4264.   
  4265.   while (p1 < end)
  4266.     {
  4267.       /* Skip over opcodes that can match nothing, and break when we get 
  4268.          to one that can't.  */
  4269.       
  4270.       switch ((re_opcode_t) *p1)
  4271.         {
  4272.     /* It's a loop.  */
  4273.         case on_failure_jump:
  4274.           p1++;
  4275.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4276.           p1 += mcnt;
  4277.           break;
  4278.           
  4279.     default: 
  4280.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  4281.             return false;
  4282.         }
  4283.     }  /* while p1 < end */
  4284.  
  4285.   return true;
  4286. } /* alt_match_null_string_p */
  4287.  
  4288.  
  4289. /* Deals with the ops common to group_match_null_string_p and
  4290.    alt_match_null_string_p.  
  4291.    
  4292.    Sets P to one after the op and its arguments, if any.  */
  4293.  
  4294. static boolean
  4295. common_op_match_null_string_p (p, end, reg_info)
  4296.     unsigned char **p, *end;
  4297.     register_info_type *reg_info;
  4298. {
  4299.   int mcnt;
  4300.   boolean ret;
  4301.   int reg_no;
  4302.   unsigned char *p1 = *p;
  4303.  
  4304.   switch ((re_opcode_t) *p1++)
  4305.     {
  4306.     case no_op:
  4307.     case begline:
  4308.     case endline:
  4309.     case begbuf:
  4310.     case endbuf:
  4311.     case wordbeg:
  4312.     case wordend:
  4313.     case wordbound:
  4314.     case notwordbound:
  4315. #ifdef emacs
  4316.     case before_dot:
  4317.     case at_dot:
  4318.     case after_dot:
  4319. #endif
  4320.       break;
  4321.  
  4322.     case start_memory:
  4323.       reg_no = *p1;
  4324.       ret = group_match_null_string_p (&p1, end, reg_info);
  4325.       
  4326.       /* Have to set this here in case we're checking a group which
  4327.          contains a group and a back reference to it.  */
  4328.  
  4329.       if (REG_MATCH_NULL_STRING_P (reg_info[reg_no])
  4330.           == MATCH_NOTHING_UNSET_VALUE)
  4331.         REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  4332.  
  4333.       if (!ret)
  4334.         return false;
  4335.       break;
  4336.           
  4337.     /* If this is an optimized succeed_n for zero times, make the jump.  */
  4338.     case no_pop_jump:
  4339.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4340.       if (mcnt >= 0)
  4341.         p1 += mcnt;
  4342.       else
  4343.         return false;
  4344.       break;
  4345.  
  4346.     case succeed_n:
  4347.       /* Get to the number of times to succeed.  */
  4348.       p1 += 2;        
  4349.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4350.  
  4351.       if (mcnt == 0)
  4352.         {
  4353.           p1 -= 4;
  4354.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4355.           p1 += mcnt;
  4356.         }
  4357.       else
  4358.         return false;
  4359.       break;
  4360.  
  4361.     case duplicate: 
  4362.       if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  4363.         return false;
  4364.       break;
  4365.  
  4366.     case set_number_at:
  4367.       p1 += 4;
  4368.  
  4369.     default:
  4370.       /* All other opcodes mean we cannot match the empty string.  */
  4371.       return false;
  4372.   }
  4373.  
  4374.   *p = p1;
  4375.   return true;
  4376. } /* common_op_match_null_string_p */
  4377.  
  4378.  
  4379. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4380.    bytes; nonzero otherwise.  */
  4381.    
  4382. static int
  4383. bcmp_translate (s1, s2, len, translate)
  4384.      unsigned char *s1, *s2;
  4385.      register int len;
  4386.      char *translate;
  4387. {
  4388.   register unsigned char *p1 = s1, *p2 = s2;
  4389.   while (len)
  4390.     {
  4391.       if (translate[*p1++] != translate[*p2++]) return 1;
  4392.       len--;
  4393.     }
  4394.   return 0;
  4395. }
  4396.  
  4397. /* Entry points for GNU code.  */
  4398.  
  4399. /* re_compile_pattern is the GNU regular expression compiler: it
  4400.    compiles PATTERN (of length SIZE) and puts the result in BUFP.
  4401.    Returns 0 if the pattern was valid, otherwise an error string.
  4402.    
  4403.    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  4404.    are set in BUFP on entry.
  4405.    
  4406.    We call regex_compile to do the actual compilation.  */
  4407.  
  4408. const char *
  4409. re_compile_pattern (pattern, length, bufp)
  4410.      const char *pattern;
  4411.      int length;
  4412.      struct re_pattern_buffer *bufp;
  4413. {
  4414.   reg_errcode_t ret;
  4415.   
  4416.   /* GNU code is written to assume RE_NREGS registers will be set
  4417.      (and extraneous ones will be filled with -1).  */
  4418.   bufp->caller_allocated_regs = 0;
  4419.   
  4420.   /* And GNU code determines whether or not to get register information
  4421.      by passing null for the REGS argument to re_match, etc., not by
  4422.      setting no_sub.  */
  4423.   bufp->no_sub = 0;
  4424.   
  4425.   /* Match anchors at newline.  */
  4426.   bufp->newline_anchor = 1;
  4427.   
  4428.   ret = regex_compile (pattern, length, obscure_syntax, bufp);
  4429.  
  4430.   return re_error_msg[(int) ret];
  4431. }     
  4432.  
  4433. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  4434.    them if this is an Emacs or POSIX compilation.  */
  4435.  
  4436. #if !defined (emacs) && !defined (_POSIX_SOURCE)
  4437.  
  4438. static struct re_pattern_buffer re_comp_buf;
  4439.  
  4440. const char *
  4441. re_comp (s)
  4442.     const char *s;
  4443. {
  4444.   reg_errcode_t ret;
  4445.   
  4446.   if (!s)
  4447.     {
  4448.       if (!re_comp_buf.buffer)
  4449.     return "No previous regular expression";
  4450.       return 0;
  4451.     }
  4452.  
  4453.   if (!re_comp_buf.buffer)
  4454.     {
  4455.       re_comp_buf.buffer = (unsigned char *) malloc (200);
  4456.       if (re_comp_buf.buffer == NULL)
  4457.         return "Memory exhausted";
  4458.       re_comp_buf.allocated = 200;
  4459.  
  4460.       re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  4461.       if (re_comp_buf.fastmap == NULL)
  4462.     return "Memory exhausted";
  4463.     }
  4464.  
  4465.   /* Match anchors at newlines.  */
  4466.   re_comp_buf.newline_anchor = 1;
  4467.  
  4468.   ret = regex_compile (s, strlen (s), obscure_syntax, &re_comp_buf);
  4469.   
  4470.   return re_error_msg[(int) ret];
  4471. }
  4472.  
  4473.  
  4474. int
  4475. re_exec (s)
  4476.     const char *s;
  4477. {
  4478.   const int len = strlen (s);
  4479.   return 0 <= re_search (&re_comp_buf, s, len, 0, len, 
  4480.                  (struct re_registers *) 0);
  4481. }
  4482. #endif /* not emacs and not _POSIX_SOURCE */
  4483.  
  4484. /* Entry points compatible with POSIX regex library.  Don't define these
  4485.    for Emacs.  */
  4486.  
  4487. #ifndef emacs
  4488.  
  4489. /* regcomp takes a regular expression as a string and compiles it.
  4490.  
  4491.    PREG is a regex_t *.  We do not expect any fields to be initialized,
  4492.    since POSIX says we shouldn't.  Thus, we set
  4493.  
  4494.      `buffer' to the compiled pattern;
  4495.      `used' to the length of the compiled pattern;
  4496.      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  4497.        REG_EXTENDED bit in CFLAGS is set; otherwise, to
  4498.        RE_SYNTAX_POSIX_BASIC;
  4499.      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  4500.      `fastmap' and `fastmap_accurate' to zero;
  4501.      `re_nsub' to the number of subexpressions in PATTERN.
  4502.  
  4503.    PATTERN is the address of the pattern string.
  4504.  
  4505.    CFLAGS is a series of bits which affect compilation.
  4506.  
  4507.      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  4508.      use POSIX basic syntax.
  4509.  
  4510.      If REG_NEWLINE is set, then . and [^...] don't match newline.
  4511.      Also, regexec will try a match beginning after every newline.
  4512.  
  4513.      If REG_ICASE is set, then we considers upper- and lowercase
  4514.      versions of letters to be equivalent when matching.
  4515.  
  4516.      If REG_NOSUB is set, then when PREG is passed to regexec, that
  4517.      routine will report only success or failure, and nothing about the
  4518.      registers.
  4519.  
  4520.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  4521.    the return codes and their meanings.)  */
  4522.  
  4523. int
  4524. regcomp (preg, pattern, cflags)
  4525.     regex_t *preg;
  4526.     const char *pattern; 
  4527.     int cflags;
  4528. {
  4529.   reg_errcode_t ret;
  4530.   unsigned syntax
  4531.     = cflags & REG_EXTENDED ? RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  4532.  
  4533.   /* regex_compile will allocate the space for the compiled pattern.  */
  4534.   preg->buffer = 0;
  4535.   
  4536.   /* Don't bother to use a fastmap when searching.  This simplifies the
  4537.      REG_NEWLINE case: if we used a fastmap, we'd have to put all the
  4538.      characters after newlines into the fastmap.  This way, we just try
  4539.      every character.  */
  4540.   preg->fastmap = 0;
  4541.   
  4542.   if (cflags & REG_ICASE)
  4543.     {
  4544.       unsigned i;
  4545.       
  4546.       preg->translate = (char *) malloc (CHAR_SET_SIZE);
  4547.       if (preg->translate == NULL)
  4548.         return (int) REG_ESPACE;
  4549.  
  4550.       /* Map uppercase characters to corresponding lowercase ones.  */
  4551.       for (i = 0; i < CHAR_SET_SIZE; i++)
  4552.         preg->translate[i] = isupper (i) ? tolower (i) : i;
  4553.     }
  4554.   else
  4555.     preg->translate = NULL;
  4556.  
  4557.   /* If REG_NEWLINE is set, newlines are treated differently.  */
  4558.   if (cflags & REG_NEWLINE)
  4559.     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
  4560.       syntax &= ~RE_DOT_NEWLINE;
  4561.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  4562.       /* It also changes the matching behavior.  */
  4563.       preg->newline_anchor = 1;
  4564.     }
  4565.   else
  4566.     preg->newline_anchor = 0;
  4567.  
  4568.   preg->no_sub = !!(cflags & REG_NOSUB);
  4569.  
  4570.   /* POSIX says a null character in the pattern terminates it, so we 
  4571.      can use strlen here in compiling the pattern.  */
  4572.   ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  4573.   
  4574.   /* POSIX doesn't distinguish between an unmatched open-group and an
  4575.      unmatched close-group: both are REG_EPAREN.  */
  4576.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  4577.   
  4578.   return (int) ret;
  4579. }
  4580.  
  4581.  
  4582. /* regexec searches for a given pattern, specified by PREG, in the
  4583.    string STRING.
  4584.    
  4585.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  4586.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  4587.    least NMATCH elements, and we set them to the offsets of the
  4588.    corresponding matched substrings.
  4589.    
  4590.    EFLAGS specifies `execution flags' which affect matching: if
  4591.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  4592.    string; if REG_NOTEOL is set, then $ does not match at the end.
  4593.    
  4594.    We return 0 if we find a match and REG_NOMATCH if not.  */
  4595.  
  4596. int
  4597. regexec (preg, string, nmatch, pmatch, eflags)
  4598.     const regex_t *preg;
  4599.     const char *string; 
  4600.     size_t nmatch; 
  4601.     regmatch_t pmatch[]; 
  4602.     int eflags;
  4603. {
  4604.   int ret;
  4605.   struct re_registers regs;
  4606.   regex_t private_preg;
  4607.   int len = strlen (string);
  4608.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  4609.  
  4610.   private_preg = *preg;
  4611.   
  4612.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  4613.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  4614.   
  4615.   /* The user has told us how many registers to return information
  4616.      about, via `nmatch'.  We have to pass that on to the matching
  4617.      routines.  */
  4618.   private_preg.caller_allocated_regs = 1;
  4619.   
  4620.   if (want_reg_info)
  4621.     {
  4622.       regs.num_regs = nmatch;
  4623.       regs.start = TALLOC (nmatch, regoff_t);
  4624.       regs.end = TALLOC (nmatch, regoff_t);
  4625.       if (regs.start == NULL || regs.end == NULL)
  4626.         return (int) REG_NOMATCH;
  4627.     }
  4628.  
  4629.   /* Perform the searching operation.  */
  4630.   ret = re_search (&private_preg, string, len,
  4631.                    /* start: */ 0, /* range: */ len,
  4632.                    want_reg_info ? ®s : NULL);
  4633.   
  4634.   /* Copy the register information to the POSIX structure.  */
  4635.   if (want_reg_info)
  4636.     {
  4637.       if (ret >= 0)
  4638.         {
  4639.           unsigned r;
  4640.  
  4641.           for (r = 0; r < nmatch; r++)
  4642.             {
  4643.               pmatch[r].rm_so = regs.start[r];
  4644.               pmatch[r].rm_eo = regs.end[r];
  4645.             }
  4646.         }
  4647.  
  4648.       /* If we needed the temporary register info, free the space now.  */
  4649.       free (regs.start);
  4650.       free (regs.end);
  4651.     }
  4652.  
  4653.   /* We want zero return to mean success, unlike `re_search'.  */
  4654.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  4655. }
  4656.  
  4657.  
  4658. /* Returns a message corresponding to an error code, ERRCODE, returned
  4659.    from either regcomp or regexec.   */
  4660.  
  4661. size_t
  4662. regerror (errcode, preg, errbuf, errbuf_size)
  4663.     int errcode;
  4664.     const regex_t *preg;
  4665.     char *errbuf;
  4666.     size_t errbuf_size;
  4667. {
  4668.   const char *msg
  4669.     = re_error_msg[errcode] == NULL ? "Success" : re_error_msg[errcode];
  4670.   size_t msg_size = strlen (msg) + 1; /* Includes the null.  */
  4671.   
  4672.   if (errbuf_size != 0)
  4673.     {
  4674.       if (msg_size > errbuf_size)
  4675.         {
  4676.           strncpy (errbuf, msg, errbuf_size - 1);
  4677.           errbuf[errbuf_size - 1] = 0;
  4678.         }
  4679.       else
  4680.         strcpy (errbuf, msg);
  4681.     }
  4682.  
  4683.   return msg_size;
  4684. }
  4685.  
  4686.  
  4687. /* Free dynamically allocated space used by PREG.  */
  4688.  
  4689. void
  4690. regfree (preg)
  4691.     regex_t *preg;
  4692. {
  4693.   if (preg->buffer != NULL)
  4694.     free (preg->buffer);
  4695.   preg->buffer = NULL;
  4696.   
  4697.   preg->allocated = 0;
  4698.   preg->used = 0;
  4699.  
  4700.   if (preg->fastmap != NULL)
  4701.     free (preg->fastmap);
  4702.   preg->fastmap = NULL;
  4703.   preg->fastmap_accurate = 0;
  4704.  
  4705.   if (preg->translate != NULL)
  4706.     free (preg->translate);
  4707.   preg->translate = NULL;
  4708. }
  4709.  
  4710. #endif /* not emacs  */
  4711.  
  4712. #ifdef test
  4713.  
  4714. #include <stdio.h>
  4715.  
  4716. /* Indexed by a character, gives the upper case equivalent of the
  4717.    character.  */
  4718.  
  4719. char upcase[0400] = 
  4720.   { 000, 001, 002, 003, 004, 005, 006, 007,
  4721.     010, 011, 012, 013, 014, 015, 016, 017,
  4722.     020, 021, 022, 023, 024, 025, 026, 027,
  4723.     030, 031, 032, 033, 034, 035, 036, 037,
  4724.     040, 041, 042, 043, 044, 045, 046, 047,
  4725.     050, 051, 052, 053, 054, 055, 056, 057,
  4726.     060, 061, 062, 063, 064, 065, 066, 067,
  4727.     070, 071, 072, 073, 074, 075, 076, 077,
  4728.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  4729.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  4730.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  4731.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  4732.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  4733.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  4734.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  4735.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  4736.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  4737.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  4738.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  4739.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  4740.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  4741.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  4742.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  4743.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  4744.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  4745.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  4746.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  4747.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  4748.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  4749.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  4750.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  4751.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  4752.   };
  4753.  
  4754.  
  4755. /* Use this to run interactive tests.  */
  4756.  
  4757. void
  4758. main (argc, argv)
  4759.      int argc;
  4760.      char **argv;
  4761. {
  4762.   char pat[500];
  4763.   struct re_pattern_buffer buf;
  4764.   int i;
  4765.   char c;
  4766.   char fastmap[(1 << BYTEWIDTH)];
  4767.  
  4768.   /* Allow a command argument to specify the style of syntax.  */
  4769.   if (argc > 1)
  4770.     re_set_syntax (atoi (argv[1]));
  4771.  
  4772.   buf.allocated = 40;
  4773.   buf.buffer = (unsigned char *) malloc (buf.allocated);
  4774.   buf.fastmap = fastmap;
  4775.   buf.translate = upcase;
  4776.  
  4777.   for (;;)
  4778.     {
  4779.       printf ("Pattern = ");
  4780.       gets (pat);
  4781.  
  4782.       if (*pat)
  4783.     {
  4784.           void printchar ();
  4785.           re_compile_pattern (pat, strlen (pat), &buf);
  4786.  
  4787.       for (i = 0; i < buf.used; i++)
  4788.         printchar (buf.buffer[i]);
  4789.  
  4790.       putchar ('\n');
  4791.  
  4792.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  4793.  
  4794.       re_compile_fastmap (&buf);
  4795.       printf ("Allowed by fastmap: ");
  4796.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  4797.         if (fastmap[i]) printchar (i);
  4798.       putchar ('\n');
  4799.     }
  4800.  
  4801.       printf ("String = ");
  4802.       gets (pat);    /* Now read the string to match against */
  4803.  
  4804.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  4805.       printf ("Match value %d.\n\n", i);
  4806.     }
  4807. }
  4808.  
  4809.  
  4810. #if 0
  4811. /* We have a fancier version now, compiled_pattern_printer.  */
  4812. print_buf (bufp)
  4813.      struct re_pattern_buffer *bufp;
  4814. {
  4815.   int i;
  4816.  
  4817.   printf ("buf is :\n----------------\n");
  4818.   for (i = 0; i < bufp->used; i++)
  4819.     printchar (bufp->buffer[i]);
  4820.   
  4821.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  4822.   
  4823.   printf ("Allowed by fastmap: ");
  4824.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  4825.     if (bufp->fastmap[i])
  4826.       printchar (i);
  4827.   printf ("\nAllowed by translate: ");
  4828.   if (bufp->translate)
  4829.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  4830.       if (bufp->translate[i])
  4831.     printchar (i);
  4832.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  4833.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  4834. }
  4835. #endif /* 0 */
  4836.  
  4837.  
  4838. void
  4839. printchar (c)
  4840.      char c;
  4841. {
  4842.   if (c < 040 || c >= 0177)
  4843.     {
  4844.       putchar ('\\');
  4845.       putchar (((c >> 6) & 3) + '0');
  4846.       putchar (((c >> 3) & 7) + '0');
  4847.       putchar ((c & 7) + '0');
  4848.     }
  4849.   else
  4850.     putchar (c);
  4851. }
  4852. #endif /* test */
  4853.  
  4854. /*
  4855. Local variables:
  4856. make-backup-files: t
  4857. version-control: t
  4858. trim-versions-without-asking: nil
  4859. End:
  4860. */
  4861.